这是Operation Queue完成块的正确用法吗?

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)

gca*_*amp 18

就是这样.[NSOperationQueue mainQueue]如果要为完成块使用操作队列而不是GCD,也可以使用.