这是操作队列完成块的正确用法吗?
|
我是第一次使用Objective-C块和操作队列。主界面显示微调器时,我正在加载一些远程数据。我正在使用完成块来告诉表重新加载其数据。如文档所述,完成块不会在主线程上运行,因此表将重新加载数据,但不会重新绘制视图,直到您在主线程上执行了某些操作(例如拖动表)。
我现在使用的解决方案是调度队列,这是从完成块刷新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];
更新资料
根据@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]; }];
};
没有找到相关结果
已邀请:
1 个回复
倪蕊悲潍