Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

RestKit Makes UI Unresponsive

Tags:

ios

restkit

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!

like image 977
threejeez Avatar asked Mar 28 '26 05:03

threejeez


1 Answers

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];
like image 91
wattson12 Avatar answered Mar 29 '26 22:03

wattson12



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!