RestKit使UI无响应

时间:2012-12-14 19:46:44

标签: ios restkit

我正在使用RestKit版本0.2,当我按如下方式调用RKRequestOperation时,我看到它阻止了UI(意思是,UI变得不连贯/无响应):

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

更多背景知识:

我这样做是为了将新模型视图加载到无限UIScrollView中。我检测到用户滚动到视图底部(坐标逻辑编辑)时,使用上面的RestKit加载下一组视图,当模型返回时,我将它们加载到addModelsToView中的滚动视图中。即使我发表评论addModelsToView,仍然会出现断断续续的逻辑,所以我确定它与RestKit有关(至少我是如何使用它的。)

根据我对RestKit的理解,它确实是异步加载的,所以我很难找到原因/发生波动的原因。

提前致谢!

1 个答案:

答案 0 :(得分:7)

start上调用NSOperation会在您调用它的同一个线程上开始同步操作。所以你在主线程上执行这个下载,这将阻止UI更新

您应该将操作添加到队列

RestKit github页面有这个例子:

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