4

我正在使用 RESTKIT 对象管理器从我的服务器获取信息。我的实现代码片段如下:

-(void)getObjects
{
    //Instantiate the RestKit Object Manager
    RKObjectManager *sharedManager = [RKObjectManager sharedManager];

    //show the spinner
    [self showLoading];

    //call server with the resourcepath
    [sharedManager loadObjectsAtResourcePath:self.resourcePath delegate:self];
}

- (void)objectLoader:(RKObjectLoader*)objectLoader didLoadObjects:(NSArray*)objects 
{

    // handling in scenarios of empty arrays
    if ( [objects count]==0 ){
        [self hideLoading];
        if (emptyHandler){
            emptyHandler();
        }else{
            [self standardEmptyHandling];            
        }
        return;
    }

    // planned failure
    if ( [[objects objectAtIndex:0] isKindOfClass:[Failure class]]){
        NSAssert([objects count]==1,@"object returned is type failure, but there are more than one object in it");
        failureObject=[objects objectAtIndex:0];
        [self hideLoading];
        [self standardErrorHandling];
        return;
    }

    //return completion block to caller
    completionHandler(objects);

}

但是,在某些情况下,可能会出现服务器错误或可达性错误,这会导致进程在终止之前继续尝试很长时间(微调器将显示更长的时间_。

有没有办法在我的实现中设置超时持续时间,以便如果服务器在 20 秒内没有响应,我可以提示用户重试?

4

2 回答 2

11

现在,RestKit 贡献者已在此拉取请求https://github.com/RestKit/RestKit/pull/491中解决了此问题,并且可以轻松设置如下:

RKObjectManager *objectManager = [RKObjectManager objectManagerWithBaseURL:@"http://..."];
objectManager.client.timeoutInterval = 30.0; // 30 seconds
于 2012-03-27T10:31:50.380 回答
1

Apple对URL 请求的默认超时为60 秒。

这是关于 RestKit 中未决问题的讨论:

http://groups.google.com/group/restkit/browse_thread/thread/8672eba8b1901f5d

NSTimer可能是一种简单的方法。

#pragma mark - RKRequestDelegate
- (void)requestDidStartLoad:(RKRequest *)request {
   [NSTimer scheduledTimerWithTimeInterval:20.0
       target:self
       selector:@selector(handleRequestTimeout)
       userInfo:nil
       repeats:NO];
}
于 2011-11-30T16:10:40.860 回答