RestKit makes UI immune

I am using RestKit version 0.2, and I see that it blocks the user interface (this means the user interface is becoming mutable / not responding) when I call RKRequestOperation as follows:

- (void)scrollViewWillEndDragging:(UIScrollView *)scrollView withVelocity:(CGPoint)velocity targetContentOffset:(inout CGPoint *)targetContentOffset { NSString *urlString = [NSString stringWithFormat:@"http://localhost:8080/models?offset=%d&rows=%d", _offset, _numRows]; NSURL *url = [NSURL URLWithString:urlString]; NSURLRequest *request = [NSURLRequest requestWithURL:url]; RKObjectRequestOperation *operation = [[RKObjectRequestOperation alloc] initWithRequest:request responseDescriptors:@[_responseDescriptor]]; [operation setCompletionBlockWithSuccess:^(RKObjectRequestOperation *operation, RKMappingResult *result) { NSLog(@"Got models: %@", [result array]); [self addModelsToView:[results array]]; } failure:^(RKObjectRequestOperation *operation, NSError *error) { NSLog(@"FAILED!"); }]; [operation start]; } 

A bit more background:

I do this to load new model views into an infinite UIScrollView . I detect when the user scrolls to the bottom of the view (coordinate logic corrected), use RestKit, as indicated above, to load the next set of views, and when the models return, I load them into the scroll in addModelsToView . Even when I comment on addModelsToView , the volatile logic remains, so I'm sure this has something to do with RestKit (or how I use it, at least).

From what I understand about RestKit, it is that it loads asynchronously, so it's hard for me to find out why / where the volatility is happening.

Thanks in advance!

+3
source share
1 answer

The start call on NSOperation starts a synchronous operation in the same thread from which you call it. Thus, you are doing this download in the main thread, which will block the user interface updates.

You must add the operation to the queue.

The RestKit github page shows an example:

 RKObjectManager *manager = [RKObjectManager managerWithBaseURL:[NSURL URLWithString:@"http://restkit.org"]; NSURLRequest *request = [NSURLRequest requestWithURL:[NSURL URLWithString:@"http://restkit.org/articles/1234.json"]]; RKObjectRequestOperation *operation = [[RKObjectRequestOperation alloc] initWithRequest:request responseDescriptors:@[responseDescriptor]]; [manager enqueueObjectRequestOperation:operation]; 
+7
source

All Articles