I am implementing a timeout for the RKObjectManager. My code snippet is as follows:
-(void)getObjects
{
RKObjectManager *sharedManager = [RKObjectManager sharedManager];
[self showLoading];
[sharedManager loadObjectsAtResourcePath:self.resourcePath delegate:self];
// Setting timeout here. goto failure
self.nTimer = [NSTimer scheduledTimerWithTimeInterval:TIMEOUT_INTERVAL target:self selector:@selector(didEncounterError) userInfo:nil repeats:NO];
}
- (void) didEncounterError
{
[self hideLoading];
[self standardErrorHandling];
//invalidate timer, this is done to ensure that if error occurs before timer expiry time, the error will not show again when timer is up (ISSUE HERE)
[self.nTimer invalidate];
}
- (void)objectLoader:(RKObjectLoader*)objectLoader didLoadObjects:(NSArray*)objects
{
....
//invalidate timer if load is successful (no issue here)
[self.nTimer invalidate];
}
- (void)objectLoader:(RKObjectLoader*)objectLoader didFailWithError:(NSError*)error
{
....
//trigger encounter error method
[self didEncounterError];
}
In the above implementation, I will always invalidate the timer in the “encounter error” method. This is to mitigate cases whereby error has occurred before the timer expires. I want to invalidate the timer in this case to prevent the error message from popping up again.
However, I am still getting the error message a second time after the error has occurred (before timer expires). It seems like the invalidation in the “encounter error” method didnt work. Any advise on what is wrong with my code?
The timer invalidation should happen on the thread where it is scheduled, in above case it is called on another thread (call backs). Can you have a method that does this invalidation and call that method using ‘performSelectorOnMainThread’ at your call back methods?