1

我正在为 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];
}

在上面的实现中,我总是会在“遇到错误”方法中使定时器失效。这是为了减轻在计时器到期之前发生错误的情况。在这种情况下,我想使计时器无效,以防止再次弹出错误消息。

但是,在发生错误后(在计时器到期之前),我仍然第二次收到错误消息。“遇到错误”方法中的失效似乎不起作用。关于我的代码有什么问题的任何建议?

4

1 回答 1

7

计时器失效应该发生在调度它的线程上,在上述情况下,它会在另一个线程上调用(回调)。你能有一个方法来做这个失效并在你的回调方法中使用'performSelectorOnMainThread'调用该方法吗?

于 2011-10-11T06:13:30.933 回答