cancelPreviousPerformRequestWithTarget is not canceling my previously delayed thread started with pe
- by jmurphy
Hello,
I've launched a delayed thread using performSelector but the user still has the ability to hit the back button on the current view causing dealloc to be called. When this happens my thread still seems to be called which causes my app to crash because the properties that thread is trying to write to have been released. To solve this I am trying to call cancelPreviousPerformRequestsWithTarget to cancel the previous request but it doesn't seem to be working. Below are some code snippets.
- (void) viewDidLoad {
[self performSelector:@selector(myStopUpdatingLocation) withObject:nil afterDelay:6];
}
(void)viewWillDisappear:(BOOL)animated {
[NSObject cancelPreviousPerformRequestsWithTarget:self selector:@selector(myStopUpdatingLocation) object:nil];
}
Am I doing something incorrect here? The method myStopUpdatingLocation is defined in the same class that I'm calling the perform requests.
A little more background. The function that I'm trying to implement is to find a users location, search google for some locations around that location and display several annotations on the map. On viewDidLoad I start updating the location with CLLocationManager. I've build in a timeout after 6 seconds if I don't get my desired accuracy within the timeout and I'm using a performSelector to do this. What can happen is the user clicks the back button in the view and this thread will still execute even though all my properties have been released causing a crash.
Thanks in advance!
James