在后台处理方法中使用相同的调度队列 [英] using the same dispatch queue in a method for background processing

查看:79
本文介绍了在后台处理方法中使用相同的调度队列的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个方法可以更新表中需要一段时间的两个部分。我想做类似的事情:

I have a method that updates two sections in a table that takes awhile. I want to do something like:

    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,那可以吗?谢谢。

If I use the same lowQueue in the same method, is that ok? Thanks.

推荐答案

是的,你可以用同样的方法使用 lowQueue 。当你抓住 DISPATCH_QUEUE_PRIORITY_LOW 全局队列并在 lowQueue 中存储对它的引用,你可以继续在其上排队多个 dispatch_async GCD调用。每次调用 dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_LOW,0)时,你都会得到一个确切的参考同一个调度队列。

Yes, you can use lowQueue in the same method. When you grab the DISPATCH_QUEUE_PRIORITY_LOW global queue and store a reference to it in lowQueue, you can continue to enqueue additional blocks on it with multiple dispatch_async GCD calls. Every time you call dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_LOW, 0), you'll get back a reference to the exact same dispatch queue.

由于所有全局调度队列都是并发队列,因此两个任务中的每个块都将出列并同时执行,前提是GCD确定这是在运行时对系统最有效(给定系统负载,可用的CPU内核,当前正在执行的其他线程数等)。

Since all the global dispatch queues are concurrent queues, each block from both of your two tasks will be dequeued and executed simultaneously, provided that GCD determines this is most efficient for the system at runtime (given system load, CPU cores available, number of other threads currently executing, etc).

这篇关于在后台处理方法中使用相同的调度队列的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆