ios 创建自定义顺序全局调度队列

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/18000279/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-31 00:35:26  来源:igfitidea点击:

create a custom sequential global dispatch queue

iosobjective-cgrand-central-dispatch

提问by pvllnspk

In many places in my app I use the next code to perform background tasks and notify the main thread:

在我的应用程序的许多地方,我使用下一个代码来执行后台任务并通知主线程:

dispatch_queue_t backgroundQueue = dispatch_queue_create("dispatch_queue_#1", 0);
    dispatch_async(backgroundQueue, ^{

   dispatch_async(dispatch_get_main_queue(), ^{


        });
    });

Is it possible to create a backgroundQueue in one place (where does the best way?) and use it later? I know about the system global queue, but ordering is important for me.

是否可以在一个地方创建一个 backgroundQueue(最好的方法在哪里?)并在以后使用它?我知道系统全局队列,但排序对我很重要。

回答by Catfish_Man

Something like this should work fine:

这样的事情应该可以正常工作:

dispatch_queue_t backgroundQueue() {
    static dispatch_once_t queueCreationGuard;
    static dispatch_queue_t queue;
    dispatch_once(&queueCreationGuard, ^{
        queue = dispatch_queue_create("com.something.myapp.backgroundQueue", 0);
    });
    return queue;
}

回答by AllanXing

queue = dispatch_queue_create("com.something.myapp.backgroundQueue", 0);

Preceding is Serial Queue,if you want create concurrent queue,you can use DISPATCH_QUEUE_CONCURRENT.

前面是串行队列,如果要创建并发队列,可以使用 DISPATCH_QUEUE_CONCURRENT。

In iOS 5 and later, you can create concurrent dispatch queues yourself by specifying DISPATCH_QUEUE_CONCURRENT as the queue type.

在 iOS 5 及更高版本中,您可以通过将 DISPATCH_QUEUE_CONCURRENT 指定为队列类型来自己创建并发调度队列。

dispatch_queue_t queue = dispatch_queue_create("downLoadAGroupPhoto",
                                                   DISPATCH_QUEUE_CONCURRENT);

回答by JeffRegan

You could also us an NSOperationQueue and push operations to it. To make sure the operations don't run out of order, you can set isConcurrent to NO.

您也可以使用 NSOperationQueue 并将操作推送给它。为确保操作不会无序运行,您可以将 isConcurrent 设置为 NO。

回答by Ankit Goyal

  dispatch_async( dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
                //back ground thread

                 dispatch_async( dispatch_get_main_queue(), ^{
                     // main thread
                      });
                });