开发者

Executing RestKit as an NSOperartion

I am currently migrating a project that used ASIHTTPRequest and SBJson to RestKit.

The previous implementation was using an NSOperation to make the HTTP Request, parse the JSON object and make the necessary calls to the Core Data API.

I have re-factored this as follows:

@implementation UpdateBeers

#pragma mark - NSOperation

- (void)main {
    [[RKClient sharedClient] get:@"/beers" delegate:self]; 
}

- (void)request:(RKRequest*)request didLoadResponse:(RKResponse*)response {  
    debug(@"didLoadResponse");
}  

- (void)request:(RKRequest *)request didFailLoadWithError:(NSError *)error {
    debug(@"%@", error);
}

#pragma mark - Memory

- (void) dealloc {
    [super dealloc];
}

@end

The following appears in the log

sending GET request to URL http://localhost:9091/api/test. HTTP Body:

The problem is the server never receives the request.

Adding the following line :

[[NSRunLoop currentRunLoop] runUntilDate:[NSDate dateWithTimeIntervalSinceNow:0.3]];

to the end开发者_JAVA技巧 of the main method solves this problem.

My question is :

Should I be executing ResKit API calls as an NSOperation and if not what are my alternatives for making calls in the background?

Thanks in advance.


Perhaps I'm not fully understanding the overall problem here ... Just about everything RestKit does with respect to loading resources from the network is already in the background. Almost everything is already asynchronous, and thus running the existing asynchronous RestKit methods inside an NSOperation is duplicative and counter productive. That being said, once RestKit tells you that it's finished downloading your data (didLoadResponse), you may want to do any subsequent post-processing in an NSOperation if that part is computationally intensive and unrelated to the UI. Otherwise, don't try to outthink RestKit, just run it as in the examples and you're good to go with asynchronous goodness.

Moreover, you may want to look at using RestKit's request queues if you plan to fire off several requests at the same time. It'll still download everything asynchronously, but it'll only run as many requests at a time as you tell it ... when my app is updating, send of about seven requests at once, but the queue will run them serially instead of parallel, thus preventing any network bandwidth issues

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜