Your problem is this line -
[NSTimer scheduledTimerWithTimeInterval:4.0f target:self selector:@selector(checkBackOnExit:) userInfo:info repeats:NO];
You don't store the new NSTimer
that is returned from scheduledTimerWithTimeInterval
anywhere so as soon as the method exits it will be deallocated. You need to add the NSTimer to a property. As you can possibly have multiple timers running concurrently I would suggest an NSMutableDictionary
.
@property (retain,nonatomic) NSMutableDictionary *timers;
Initialise this wherever appropriate (such as init
)
self.timers=[[NSMutableDictionary alloc]init];
then in didExitRegion:
[self.timers addObject:[NSTimer scheduledTimerWithTimeInterval:4.0f target:self selector:@selector(checkBackOnExit:) userInfo:info repeats:NO] forKey:gObject.geoFenceName];
In didEnterRegion:
NSTimer *timer=(NSTimer *)[self.timers objectForKey:gObject.geoFenceName]
if (timer != nil) {
[timer invalidate];
[self.timers removeObjectForKey:gObject.geoFenceName];
}
And also in your checkBackOnExit:
method
[timer invalidate];
[self.timers removeObjectForKey:gObject.geoFenceName];