Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Request timeout in restkit 0.20.0

Tags:

restkit

I didn't find any way to set the timeout interval on restkit 0.20.0.

Can anyone help to increase the timeout interval.

Thanks

like image 709
user1433374 Avatar asked Dec 13 '12 07:12

user1433374


3 Answers

Subclass RKHTTPRequestOperation and implement method

- (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];
}
like image 171
innerself Avatar answered Sep 20 '22 14:09

innerself


RestKit now use AFNetworking for it's HTTP layer, so you need to set it in the HTTPClient of Restkit. See this Issue on AFNetworking Github. Also, Matt the creator of AFNetworking does not really like the idea of opening up a timeout property easily (see his reason here)

I hope this can give you some insights!

like image 27
allaire Avatar answered Sep 20 '22 14:09

allaire


Complete Code

To be more elaborate/descriptive, my code was as follows:

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

Then (and this is the the part that helped me to know, that wasn't mentioned in other answers), register you class with RKObjectManager.

Like so (Forgive my inconsistency, this is my only segment of code in swift not objective c):

RKObjectManager.sharedManager().registerRequestOperationClass(Timeoutable);
like image 35
csga5000 Avatar answered Sep 17 '22 14:09

csga5000