3

我正在使用 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 的了解,它确实是异步加载的,所以我很难找到出现断断续续的原因/位置。

提前致谢!

4

1 回答 1

7

调用startanNSOperation会在您调用它的同一线程上开始同步操作。因此,您在主线程上执行此下载,这将阻止 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];
于 2012-12-14T19:54:44.193 回答