RestKit使UI无响应
我使用的RestKit版本0.2,我看到它阻止用户界面(意思是,用户界面变得波涛汹涌/无响应)当我打电话给RKRequestOperation如下:
- (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
。 我检测用户滚动到视图的底部(坐标逻辑redacted),使用RestKit如上加载下一组视图,当模型返回我加载到addModelsToView
滚动视图。 即使当我注释掉addModelsToView
,不稳定的逻辑仍然存在,所以我确定这是与RestKit(或至less我如何使用它)有关。
从我对RestKit的了解来看,它是asynchronous加载的,所以我很难find为什么/在哪里发生不稳定。
提前致谢!
在一个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];