R – Storing a persistent CLLocation

core-locationiphoneobjective cpersistence

I think I am missing something obvious somewhere.
I have a (CLLocation *)lastqueriedlocation defined in the header as a property and synthesized. I want to updated it in locationManager:didUPdateToLocation:fromLocation:

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

    CLLocationDistance dist = [lastQueriedLocation getDistanceFrom:newLocation];
    if (dist>1000) {
        lastQueriedLocation = newLocation;
        [self reSearch:lastQueriedLocation];
    }

    if ([resultArray count] > 0) {
        [self findAndDisplayNearestLocation:location];
    }   
}

lastQueriedLocation is alloced and init'd in viewDidLoad.

The issue is that of course lastQueriedLocation = newLocation; results in an EXC_BAD_ACCESS. So what is the correct approach to persisting the lastQueriedLocation ?

If it helps make the problem more concrete – reSearch is calling a web service fetching POI within 2km of the location – so I only want to do it when we have moved 1km … but I still want to keep the accuracy at best so I can highlight the nearest, scroll the map etc.

Best Answer

You're just setting the iVar rather than the synthesised property, which means newLocation doesn't get retained by the property.

Replace lastQueriedLocation = newLocation; with:

[self setLastQueriedLocation:newLocation];

Or, if you'd rather use dot-notation:

self.lastQueriedLocation = newLocation;
Related Topic