Search code examples
objective-cuipickerviewcllocationmanagernsmutableurlrequest

Is CLLocationManager causing this crash?


I've got a UIPickerView object that lists stores based on either a listed "home" location or one's current location (using CLLocationManager). If the latter is implemented, I make a NSMutableURLRequest to my server to get the closest stores, then update a UIPickerView with the received list.

On occasion, (and oddly enough never when I'm at the "home" location), I will use the current location, I'll see the picker update the list, then the app immediately crashes.

My picker code is simple enough:

-(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"];
    }
}

One thought was that it was providing a second, more accurate reading and that I was releasing something that I may have already released. My CLLocationManager code is:

- (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];
}
}

And the pertinent NSMutableURLRequest methods are:

- (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;
}    

}

Im curious as to why I can see the picker being updated just before the crashes. Because this happens when I'm on the road, I'm suspecting it's an accuracy thing, and location manager is sending a second result, causing the crash. Any thoughts?


Solution

  • It appears I was trying to do too much of a shortcut. When the user hit the location button, it would turn on the LocationManager, which with every result would set up an NSURLConnection to my server. I was bring to handle all this in a single class, but with the multiple results the location manager would return, NSURLConnections seemed to be uncontrolled. I have since put each location manager result in its own class and everything is working honky dory. So, no issue with the picker - mainly a memory issue with location manager and nsurlconnections.