xcode 绘制时带有 tableView reloadData 延迟的 sendAsynchronousRequest
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/8169774/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
sendAsynchronousRequest with tableView reloadData delay on draw
提问by Javier Martinez Fernandez
I'm trying to use the new sendAsynchronousRequest added in iOS5. I have a model class (File) with a method that request some data from a server, and then pass this data to the controller class (FilesController) that created the model object.
我正在尝试使用 iOS5 中添加的新 sendAsynchronousRequest。我有一个模型类 (File),其方法从服务器请求一些数据,然后将此数据传递给创建模型对象的控制器类 (FilesController)。
The model class has a method with the next code:
模型类有一个方法,代码如下:
[NSURLConnection sendAsynchronousRequest:request queue:[[NSOperationQueue alloc] init] completionHandler:^(NSURLResponse *response, NSData *data, NSError *error) {
NSArray *decodedResponse = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableContainers error:nil];
NSArray *files = [self _dictionariesToFiles:decodedResponse];
handler(files);
}];
The controller class, uses it like this:
控制器类,像这样使用它:
[File findAll:conditions completionHandler:^(NSArray *files) {
dataSource = files;
NSLog(@"set");
[self.tableView reloadData];
NSLog(@"reload");
activityIndicator.hidden = TRUE;
}];
In the console I can view immediately how the NSLogs shows the "set" and "reload" messages, but the table and the indicator doesn't change until some seconds (5-10s) have passed.
在控制台中,我可以立即查看 NSLogs 如何显示“设置”和“重新加载”消息,但表格和指示器直到几秒钟(5-10 秒)过去后才会改变。
Somebody knows where is the problem?
有人知道问题出在哪里吗?
Thanks.
谢谢。
PD: I changed the async request with a compatible synched one, and then the problem dissapear but want to use an async request for this.
PD:我用兼容的同步请求更改了异步请求,然后问题消失了,但想为此使用异步请求。
This is the compatible sync code:
这是兼容的同步代码:
NSData *responseData = [NSURLConnection sendSynchronousRequest:request returningResponse:nil error:nil];
NSArray *decodedResponse = [NSJSONSerialization JSONObjectWithData:responseData options:NSJSONReadingMutableContainers error:nil];
NSArray *files = [self _dictionariesToFiles:decodedResponse];
handler(files);
回答by XJones
you need to do [tableView reloadData]
on the main thread. All UI operations must be done on the main thread. Multiple ways to do this. One is:
你需要[tableView reloadData]
在主线程上做。所有 UI 操作都必须在主线程上完成。有多种方法可以做到这一点。一种是:
dispatch_async(dispatch_get_main_queue(), ^{
[self.tableView reloadData];
activityIndicator.hidden = TRUE;
});
EDIT: added activityIndicator to example
编辑:将活动指示器添加到示例中
回答by Rscorreia
I did have several problems using: [[NSOperationQueue alloc] init]
but using [NSOperationQueue mainQueue]
everything is perfect. Using a new Queue the execution line does not follow the natural sequence
我确实在使用时遇到了几个问题:[[NSOperationQueue alloc] init]
但是使用[NSOperationQueue mainQueue]
一切都是完美的。使用新队列,执行行不遵循自然顺序