当前位置 : 主页 > 手机开发 > ios >

ios – RestKit使UI无响应

来源:互联网 收集:自由互联 发布时间:2021-06-11
我正在使用RestKit版本0.2,当我调用RKRequestOperation时,我看到它阻止了UI(意思是,UI变得不连贯/无响应),如下所示: - (void)scrollViewWillEndDragging:(UIScrollView *)scrollView withVelocity:(CGPoint)velocity tar
我正在使用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的理解,它确实以异步方式加载,因此我无法找到原因/发生波动的原因.

提前致谢!

在NSOperation上调用start会在您调用它的同一个线程上开始同步操作.所以你在主线程上执行这个下载,这将阻止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];
网友评论