3

I'm using RestKit version 0.2 and I'm seeing it block the UI (meaning, the UI becomes choppy/unresponsive) 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'm doing this to load new model views into an infinite UIScrollView. I detect when the user scrolls to the bottom of the view (coordinate logic redacted), use RestKit as above to load the next set of views, and when the models return I load them into the scroll view in addModelsToView. Even when I comment out addModelsToView, the choppy logic remains, so I'm certain it's something to do with RestKit (or how I'm using it, at least).

From what I understand about RestKit is that it does load asynchronously, so I'm having trouble finding why/where the choppyness is occurring.

Thanks in advance!

threejeez
  • 2,314
  • 6
  • 30
  • 51

1 Answers1

7

Calling start on an NSOperation begins a synchronous operation on the same thread that you are calling it from. So you are performing this download on the main thread, which would be blocking UI updates

You should add the operation to a queue

the RestKit github page has this 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];
wattson12
  • 11,176
  • 2
  • 32
  • 34