Objective c NSTimer未失效
我正在为RKObjectManager实现一个超时。我的代码片段如下所示:Objective c NSTimer未失效,objective-c,ios,nstimer,invalidation,Objective C,Ios,Nstimer,Invalidation,我正在为RKObjectManager实现一个超时。我的代码片段如下所示: -(void)getObjects { RKObjectManager *sharedManager = [RKObjectManager sharedManager]; [self showLoading]; [sharedManager loadObjectsAtResourcePath:self.resourcePath delegate:self]; // Setting ti
-(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];
}
在上面的实现中,我将始终在“遭遇错误”方法中使计时器无效。这是为了减轻在计时器过期之前发生错误的情况。我想在这种情况下使计时器无效,以防止错误消息再次弹出
但是,在错误发生后(计时器过期之前),我仍然会第二次收到错误消息。“遭遇错误”方法中的无效性似乎不起作用。关于我的代码出了什么问题,有什么建议吗?计时器失效应该发生在它被调度的线程上,在上面的情况下,它在另一个线程上被调用(回调)。您是否可以有一个执行此无效化的方法,并在回调方法中使用“performSelectorOnMainThread”调用该方法?计时器无效化应该发生在计划的线程上,在上述情况下,它会在另一个线程上调用(回调)。您是否可以有一个执行此无效化的方法,并在回调方法中使用“performSelectorOnMainThread”调用该方法