在restkit 0.20.0中请求超时

时间:2012-12-13 07:32:49

标签: restkit

我没有找到任何方法在restkit 0.20.0上设置超时间隔。

任何人都可以帮助增加超时间隔。

由于

3 个答案:

答案 0 :(得分:6)

子类RKHTTPRequestOperation和实现方法

- (NSURLRequest *)connection:(NSURLConnection *)connection willSendRequest:(NSURLRequest *)request redirectResponse:(NSURLResponse *)redirectResponse
{
    NSMutableURLRequest *requestWithTimeout = [request mutableCopy];
    [requestWithTimeout setTimeoutInterval:30];

    return [super connection:connection willSendRequest:requestWithTimeout redirectResponse:redirectResponse];
}

答案 1 :(得分:5)

RestKit现在使用AFNetworking作为HTTP层,因此您需要在Restkit的HTTPClient中设置它。 AFNetworking Github上的See this Issue。此外,AFNetworking的创建者Matt并不喜欢轻松打开超时属性的想法(see his reason here

我希望这可以给你一些见解!

答案 2 :(得分:3)

完整代码

为了更精细/更具描述性,我的代码如下:

RKHTTPRequestOperation_Timeoutable.h

#import "RKHTTPRequestOperation.h"

@interface RKHTTPRequestOperation_Timeoutable: RKHTTPRequestOperation

- (NSURLRequest *)connection:(NSURLConnection *)connection willSendRequest:(NSURLRequest *)request redirectResponse:(NSURLResponse *)redirectResponse;

@end

RKHTTPRequestOperation_Timeoutable.m

#import "RKHTTPRequestOperation_Timeoutable.h"

@implementation RKHTTPRequestOperation_Timeoutable

- (NSURLRequest *)connection:(NSURLConnection *)connection willSendRequest:(NSURLRequest *)request redirectResponse:(NSURLResponse *)redirectResponse
{
    NSMutableURLRequest *requestWithTimeout = [request mutableCopy];
    [requestWithTimeout setTimeoutInterval:150];//2.5 minutes

    return [super connection:connection willSendRequest:requestWithTimeout redirectResponse:redirectResponse];
}

@end

然后(这是帮助我知道的那部分,在其他答案中没有提到),用RKObjectManager注册你的课程

就像这样(原谅我的不一致,这是我在 swift 而不是目标c 中的唯一代码段):

RKObjectManager.sharedManager().registerRequestOperationClass(Timeoutable);