NSTimer 未失效

发布于 2024-12-08 22:01:04 字数 1259 浏览 0 评论 0原文

我正在为 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

白云不回头 2024-12-15 22:01:04

计时器失效应该发生在调度它的线程上,在上面的情况下,它是在另一个线程上调用的(回调)。您是否可以有一个方法来执行此失效操作,并在回调方法中使用“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?

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文