Cocoa touch 这是操作队列完成块的正确用法吗?
我第一次使用Objective-C块和操作队列。我正在加载一些远程数据,而主UI显示一个微调器。我正在使用一个完成块来告诉表重新加载其数据。作为,完成块不会在主线程上运行,因此表会重新加载数据,但不会重新绘制视图,直到在主线程上执行某些操作(如拖动表) 我现在使用的解决方案是调度队列,这是从完成块刷新UI的“最佳”方法吗Cocoa touch 这是操作队列完成块的正确用法吗?,cocoa-touch,ios,nsoperationqueue,grand-central-dispatch,Cocoa Touch,Ios,Nsoperationqueue,Grand Central Dispatch,我第一次使用Objective-C块和操作队列。我正在加载一些远程数据,而主UI显示一个微调器。我正在使用一个完成块来告诉表重新加载其数据。作为,完成块不会在主线程上运行,因此表会重新加载数据,但不会重新绘制视图,直到在主线程上执行某些操作(如拖动表) 我现在使用的解决方案是调度队列,这是从完成块刷新UI的“最佳”方法吗 // define our block that will execute when the task is finished void (^jobFinish
// 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]; }];
};
就是这样。如果您想在完成块中使用操作队列而不是GCD,也可以使用
[NSOperationQueue mainQueue]
。酷,我不知道mainQueue。这样更干净、更稳定。谢谢使用[NSOperationQueue mainQueue]和dispatch_get_main_queue()之间有实际区别吗?在结果方面,没有。但在使用方式上有所不同NSOperationQueue
使用(显然)NSOperation
和GCD(dispatch\u get\u main\u queue)使用block.@gcamp但NSOperation/queue是用GCD实现的,所以无论哪种方式都是smae,否?@0xSina最终结果是相同的,是的,但API是不同的。这正是我在之前的评论中所说的。