CLLocationManager导致此崩溃吗?

时间:2012-01-06 22:26:44

标签: objective-c uipickerview cllocationmanager nsmutableurlrequest

我有一个UIPickerView对象,它根据列出的“home”位置或一个当前位置(使用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;
}    

}

我很好奇为什么我可以看到在崩溃之前更新的选择器。因为当我在路上时会发生这种情况,我怀疑这是一个准确的事情,而位置管理员正在发送第二个结果,导致崩溃。有什么想法吗?

1 个答案:

答案 0 :(得分:0)

看来我试图做太多的捷径。当用户点击位置按钮时,它将打开LocationManager,每个结果都会为我的服务器设置一个NSURLConnection。我被带到单个类中处理所有这些,但是由于位置管理器将返回多个结果,NSURLConnections似乎是不受控制的。我已经将每个位置管理器的结果放在自己的类中,并且一切都在工作。因此,选择器没有问题 - 主要是位置管理器和nsurlconnections的内存问题。