在背景处理方法中使用相同的调度队列



我有一种方法,该方法在表需要一段时间的表中更新了两个部分。我想做类似的事情:

    dispatch_queue_t lowQueue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_LOW, 0);
    dispatch_queue_t mainQueue = dispatch_get_main_queue();
    dispatch_async(lowQueue, ^{
        NSArray *tempArray = // do long running task to get the data
        dispatch_async(mainQueue, ^{
            // update the main thread
            [self.activityIndicatorView stopAnimating];
            [self.reportsTableView reloadData];
        });
    });
dispatch_async(lowQueue, ^{
NSArray *tempArray2 = // same thing, do another long task
    // similarly, update the main thread

如果我以相同的方法使用相同的低水平,那可以吗?谢谢。

是的,您可以在相同的方法中使用lowQueue。当您抓住DISPATCH_QUEUE_PRIORITY_LOW全局队列并将其引用存储在lowQueue中时,您可以继续使用多个dispatch_async GCD调用来加入其他块。每次致电dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_LOW, 0)时,您都会获得对完全相同的派遣队列的引用。

由于所有全局调度队列都是并发队列的,因此,只要GCD确定这对于系统时的系统(给定的系统加载,可用的CPU核心,可用的CPU核心),则两个任务的每个块将同时脱水和执行。当前执行的其他线程数量等)。

最新更新