如何设置RESTKIT对象管理器的超时间隔

发布于 2024-12-09 11:04:58 字数 1278 浏览 1 评论 0 原文

我正在使用 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 秒内没有响应,我可以提示用户重试?

I am using the RESTKIT Object Manager to get information from my server. The snippet of my implementation code is as follows:

-(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);

}

However there might be cases whereby there is a server error or reachability error this causing the process to continue trying for a long duration before terminating (spinner will be displayed for an extended amount of time_.

Is there a way to set a timeout duration in my implementation so that I can prompt the user an alert to try again if the server does not respond in 20 secs for example?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(2

千里故人稀 2024-12-16 11:04:58

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

RKObjectManager *objectManager = [RKObjectManager objectManagerWithBaseURL:@"http://..."];
objectManager.client.timeoutInterval = 30.0; // 30 seconds

This has now been resolved by RestKit contributors in this pull request https://github.com/RestKit/RestKit/pull/491 and can be set easily as follows:

RKObjectManager *objectManager = [RKObjectManager objectManagerWithBaseURL:@"http://..."];
objectManager.client.timeoutInterval = 30.0; // 30 seconds
格子衫的從容 2024-12-16 11:04:58

Apple 的 默认超时 ="nofollow noreferrer">URL 请求 为 60 秒。

以下是有关 RestKit 中悬而未决问题的讨论:

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

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

#pragma mark - RKRequestDelegate
- (void)requestDidStartLoad:(RKRequest *)request {
   [NSTimer scheduledTimerWithTimeInterval:20.0
       target:self
       selector:@selector(handleRequestTimeout)
       userInfo:nil
       repeats:NO];
}

Apple's default timeout for URL requests is 60 secs.

Here is a discussion about the pending issue in RestKit:

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

A NSTimer could be an easy way around.

#pragma mark - RKRequestDelegate
- (void)requestDidStartLoad:(RKRequest *)request {
   [NSTimer scheduledTimerWithTimeInterval:20.0
       target:self
       selector:@selector(handleRequestTimeout)
       userInfo:nil
       repeats:NO];
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文