不确定的行为,因为我怀疑我陷入僵局,
我有一个具有多个对象的类 – 每个对象创建一个具有相同名称的队列.我不知道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
解决方法
正如苹果公司的文件所述,标签是:
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; }
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。