Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/ruby-on-rails-4/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Objective c CLLocationManager是导致此崩溃的原因吗?_Objective C_Uipickerview_Cllocationmanager_Nsmutableurlrequest - Fatal编程技术网

Objective c CLLocationManager是导致此崩溃的原因吗?

Objective c CLLocationManager是导致此崩溃的原因吗?,objective-c,uipickerview,cllocationmanager,nsmutableurlrequest,Objective C,Uipickerview,Cllocationmanager,Nsmutableurlrequest,我有一个UIPickerView对象,它使用CLLocationManager根据列出的主位置或当前位置列出商店。如果实现了后者,我会向服务器发出NSMutableURLRequest以获取最近的存储,然后使用收到的列表更新UIPickerView 有时候,奇怪的是,当我在家的时候,我会使用当前位置,我会看到选择器更新列表,然后应用程序立即崩溃 我的选择器代码非常简单: -(NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pick

我有一个UIPickerView对象,它使用CLLocationManager根据列出的主位置或当前位置列出商店。如果实现了后者,我会向服务器发出NSMutableURLRequest以获取最近的存储,然后使用收到的列表更新UIPickerView

有时候,奇怪的是,当我在家的时候,我会使用当前位置,我会看到选择器更新列表,然后应用程序立即崩溃

我的选择器代码非常简单:

-(NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView{
return 1;
}

-(NSInteger) pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component {
    if (isHome) {
        return [storesData count];
    } else {
        return [storesDataLoc count];
    }
}

-(NSString *)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component {
    if (isHome) {
        return [[storesData objectAtIndex:row] objectForKey:@"STName"];
    } else {
        return [[storesDataLoc objectAtIndex:row] objectForKey:@"STName"];
    }
}
一个想法是,它提供了第二个更准确的读数,我正在发布一些我可能已经发布的东西。我的CLLocationManager代码是:

- (void)locationManager:(CLLocationManager *)manager didFailWithError:(NSError *)error {
if (error.code == kCLErrorLocationUnknown) {
    NSLog(@"Currently unable to retrieve location");
} else if (error.code == kCLErrorNetwork) {
    NSLog(@"Network used to retrieve location is unavailable");
} else if (error.code == kCLErrorDenied) {
    NSLog(@"Permission to retrieve location is denied");
    [locMan stopUpdatingLocation];
    [locMan release];
    locMan = nil;

    // revert segmented controller to Home position
    storeSource.selectedSegmentIndex = 0;
}
if(loadstoresconnection!=nil){
    [loadstoresconnection release];
}
networkView.hidden = TRUE;
isHome = TRUE;
}

- (void)locationManager:(CLLocationManager *)manager
didUpdateToLocation:(CLLocation *)newLocation
       fromLocation:(CLLocation *)oldLocation {

if (newLocation.horizontalAccuracy >= 0) {
    networkView.hidden = TRUE;
    if (newLocation.horizontalAccuracy < 200) {
        [locMan stopUpdatingLocation];
        [locMan release];
        locMan = nil;
    }

    //call for store list from this location
    NSString *myString = [NSString stringWithFormat:@"http://mywebsite.com/?lat=%f&lng=%f",newLocation.coordinate.latitude,newLocation.coordinate.longitude];
    NSURL *myURL = [[NSURL alloc] initWithString:[myString stringByAddingPercentEscapesUsingEncoding:NSASCIIStringEncoding]];
    NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:myURL cachePolicy:NSURLRequestReloadIgnoringLocalCacheData timeoutInterval:60];
    loadstoresconnection = [[NSURLConnection alloc] initWithRequest:request delegate:self];
}
}
相关的NSMutableURLRequest方法有:

- (void)connection:(NSURLConnection *)connection didFailWithError:(NSError *)error {
[responseData release];
[connection release];
networkView.hidden = TRUE;
isHome = YES;
//    [textView setString:@"Unable to fetch data"];
}

- (void)connectionDidFinishLoading:(NSURLConnection *)connection {
int i;
NSArray *querydata;

networkView.hidden = TRUE;
[loadstoresconnection release];
if (storesDataLoc!=nil) {
    [storesDataLoc release];
    storesDataLoc = nil;
}
storesDataLoc = [[NSMutableArray alloc] init];
NSString *txt = [[[NSString alloc] initWithData:responseData encoding: NSASCIIStringEncoding] autorelease];
[responseData release];
// put data into variables.
querydata = [txt componentsSeparatedByString:@"<-sl->"];//break up data into data sections: 0 - number of deptsections and names, 1 - list of objects
NSArray *allstoreinfo;
for (i=0; i<[querydata count]; i++) {
    allstoreinfo = [[querydata objectAtIndex:i] componentsSeparatedByString:@"<-as->"];
    [storesDataLoc addObject:[[[NSMutableDictionary alloc] initWithObjectsAndKeys:[allstoreinfo objectAtIndex:0],@"STid",[allstoreinfo objectAtIndex:1],@"STName",[allstoreinfo objectAtIndex:2],@"STAddr",nil] autorelease]];
}
if ([querydata count]>0) {
    [pickerView reload];
    [pickerView selectRow:0 inComponent:0 animated:NO];
    isHome = NO;
}    
}


我很好奇为什么我能看到选择器在崩溃前被更新。因为这种情况发生在我在路上的时候,我怀疑这是一种准确性问题,而位置管理器正在发送第二个结果,导致了撞车。有什么想法吗?

看来我想走的捷径太多了。当用户点击location按钮时,它会打开LocationManager,每次结果都会设置到我的服务器的NSURLConnection。我被要求在一个类中处理所有这些,但由于位置管理器将返回多个结果,NSURLConnections似乎无法控制。从那以后,我把每个位置经理的结果都放在了自己的课堂上,一切都很顺利。因此,选择器没有问题-主要是位置管理器和nsurlconnections的内存问题。

我假设所有位置变量都是属性。在出错期间,是否有原因将它们全部释放?在解除锁定之前,你不应该释放它们。你可以停止更新位置,但我会保留你的变量,直到你完成视图。嗨,比尔,谢谢你的回复-我发布它们是因为在同一个类中,如果用户再次按下“当前位置”按钮,我会创建新的实例。我意识到,因为没有任何东西是正确封装的,因此将每个GPS调用放入了自己的类中。Anyhoo-这就是为什么要发布这些内容。谢谢你的意见!如果这回答了您的问题,那么您应该将您的答案标记为答案。