RestKit Makes UI Unresponsive

*爱你&永不变心* 提交于 2019-12-10 18:18:47

问题


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!


回答1:


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];


来源:https://stackoverflow.com/questions/13885295/restkit-makes-ui-unresponsive

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!