【发布时间】:2013-11-18 16:24:30
【问题描述】:
我正在使用 NSURLSession 来获取填充 TableView 的值。我正在更新完成处理程序中的 TableView,但使用 [[NSThread currentThread] isMainThread] 向我显示完成处理程序没有在主线程中运行。由于我应该只从主线程更新 UI,我知道这是不正确的。有没有办法从完成处理程序触发主线程上的操作?还是使用 NSURLSession 是错误的方法?
NSURLSession *session = [NSURLSession sharedSession];
[[session dataTaskWithURL:[NSURL URLWithString:@"http://myurl"]
completionHandler:^(NSData *data, NSURLResponse *response, NSError *error) {
NSError *jsonError = nil;
NSArray* jsonUsers = [NSJSONSerialization JSONObjectWithData:data options:0 error:&jsonError];
if (jsonError) {
NSLog(@"error is %@", [jsonError localizedDescription]);
// Handle Error and return
return;
}
self.userArray = jsonUsers;
[self.userTableView reloadData];
if ([[NSThread currentThread] isMainThread]){
NSLog(@"In main thread--completion handler");
}
else{
NSLog(@"Not in main thread--completion handler");
}
}] resume];
【问题讨论】:
标签: ios multithreading cocoa ios7