Jer*_*lin 13 cocoa-touch nsoperationqueue grand-central-dispatch ios
我第一次使用Objective-C块和操作队列.我正在加载一些远程数据,而主UI显示一个微调器.我正在使用完成块告诉表重新加载其数据.正如文档中提到的那样,完成块不会在主线程上运行,因此表会重新加载数据,但在主线程上执行某些操作(例如拖动表)之前不会重新绘制视图.
我现在使用的解决方案是一个调度队列,这是从完成块刷新UI的"最佳"方法吗?
// define our block that will execute when the task is finished
void (^jobFinished)(void) = ^{
// We need the view to be reloaded by the main thread
dispatch_async(dispatch_get_main_queue(),^{
[self.tableView reloadData];
});
};
// create the async job
NSBlockOperation *job = [NSBlockOperation blockOperationWithBlock:getTasks];
[job setCompletionBlock:jobFinished];
// put it in the queue for execution
[_jobQueue addOperation:job];
Run Code Online (Sandbox Code Playgroud)
更新 Per @ gcamp的建议,完成块现在使用主操作队列而不是GCD:
// define our block that will execute when the task is finished
void (^jobFinished)(void) = ^{
// We need the view to be reloaded by the main thread
[[NSOperationQueue mainQueue] addOperationWithBlock:^{ [self.tableView reloadData]; }];
};
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
7110 次 |
| 最近记录: |