【发布时间】:2011-11-17 15:30:20
【问题描述】:
我正在尝试使用 iOS5 中添加的新 sendAsynchronousRequest。 我有一个模型类(File),它的方法从服务器请求一些数据,然后将这些数据传递给创建模型对象的控制器类(FilesController)。
模型类有一个方法,代码如下:
[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);
}];
控制器类,像这样使用它:
[File findAll:conditions completionHandler:^(NSArray *files) {
dataSource = files;
NSLog(@"set");
[self.tableView reloadData];
NSLog(@"reload");
activityIndicator.hidden = TRUE;
}];
在控制台中,我可以立即查看 NSLogs 如何显示“设置”和“重新加载”消息,但表格和指示器直到几秒钟(5-10 秒)过去后才会改变。
有人知道问题出在哪里吗?
谢谢。
PD:我用兼容的同步请求更改了异步请求,然后问题消失了,但我想为此使用异步请求。
这是兼容的同步码:
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);
【问题讨论】:
标签: objective-c ios xcode ios5