using the same dispatch queue in a method for background processing

僤鯓⒐⒋嵵緔 提交于 2019-12-21 21:34:59

问题


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

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


回答1:


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.

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).



来源:https://stackoverflow.com/questions/13275180/using-the-same-dispatch-queue-in-a-method-for-background-processing

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!