NSTimer 未失效
我正在为 RKObjectManager 实施超时。我的代码片段如下:
-(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];
}
在上面的实现中,我总是在“遇到错误”方法中使计时器无效。这是为了减轻在计时器到期之前发生错误的情况。在这种情况下,我想使计时器无效,以防止再次弹出错误消息。
但是,在错误发生后(在计时器到期之前),我仍然再次收到错误消息。看来“遇到错误”方法中的失效不起作用。对我的代码有什么问题有什么建议吗?
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?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
计时器失效应该发生在调度它的线程上,在上面的情况下,它是在另一个线程上调用的(回调)。您是否可以有一个方法来执行此失效操作,并在回调方法中使用“performSelectorOnMainThread”调用该方法?
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?