ios - 创建具有相同名称的串行队列的 2 个对象是否共享相同的队列

标签 ios objective-c grand-central-dispatch

不确定行为,因为我怀疑我陷入了僵局,

我有一个包含多个对象的类 - 每个对象都创建一个同名队列。我不确定 GCD 是否在对象之间重用相同的队列,或者它们是否只是共享相同的名称。

例如

@interface MyClass

-(void)doSomeWork
@property (nonatomic,strong) dispatch_queue_t myQueue;

@end

@implementation MyClass

-(id)init
{
  self = [super init];
  self.myQueue = dispatch_queue_create("MyQueue",DISPATCH_QUEUE_SERIAL);
  return self;
}

-(void)doSomeWork
{
  dispatch_async(self.myQueue,^{
    // some long running work
  });
}

@end


@interface SomeClassWhichCreatesALotOfObjects

@end


@implementation SomeClassWhichCreatesALotOfObjects

-(void)someMethod
{
  for(int i = 0; i < 10000; i++)
  {
    MyClass *object = [MyClass new];
    [object doSomeWork]; // are these running in serial to each other or are each offset to the queue their object has created? Can't understand from the debugger
  }
}
@end

最佳答案

正如 Apple 的文档所述,标签是:

A string label to attach to the queue to uniquely identify it in debugging tools such as Instruments...

仅用作提示,仅此而已。

编辑

这是使用共享队列所需的代码。

+ (dispatch_queue_t)sharedQueue
{
    static dispatch_queue_t sharedQueue;
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        sharedQueue = dispatch_queue_create("MyQueue", NULL);
    });
    return sharedQueue;
}

关于ios - 创建具有相同名称的串行队列的 2 个对象是否共享相同的队列,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19421283/

相关文章:

ios - tableview 单元格操作问题 xcode

ios - Alamofire swift 库版本 4.4 未安装在 swift 3.0.2 项目中

Objective-C 静态类级别变量

ios - 数组获得零 GCD

ios - [UIViewController initWithCoder :] causes debugger to freeze 上的符号断点

ios - swift 中的时间比较

ios - 如何在表格 View 单元格上应用复选标记

objective-c - 如何在不更改字体本身的情况下更改 uilabel 的字体大小?

ios - 后台线程崩溃

ios - 对方法进行排队,以便在 iOS 应用程序中更快地加载 View