dispatch_get_current_queue 废弃

由于在iOS7后dispatch_get_current_queue被废弃,本文探讨了其原因和替代方案。在非GCD线程中,该函数返回的是com.apple.root.default-overcommit-priority队列,这可能与实际执行的队列不符。文章详细分析了不同线程类型与dispatch_queue的关系,并列举了各种API获取的队列及其应用场景。
摘要由CSDN通过智能技术生成

由于iOS7以后 dispatch_get_current_queue 被废弃,所以需要寻找一个替代的方案。

发现 dispatch_get_current_queue 并没有字面上那么简单。

这个函数一般都会跟 dispatch_async 等API配合,

但是试想一下,我们自己创建的线程(比如 NSThread)跟 dispatch_queue_t 没有关系,

那么在我们的线程中调用 dispatch_get_current_queue 会返回什么呢?

     [NSThread detachNewThreadSelector:@selector(onPlay:) toTarget:self withObject:nil];

     - (void)onPlay
     {
         dispatch_queue_t dispatch_queue = dispatch_get_current_queue(); // 这里会返回什么?
     }

在上面的代码中,我们并没有将onPlay显示投递到某一个dispatch_queue中,

但是dispatch_get_current_queue还是会返回一个dispatch_queue,

一个名字叫 com.apple.root.default-overcommit-priority 的 dispatch_queue!

但是!

如果在 onPlay 中打断点,你会发现,onPlay 并不在 com.apple.root.default-overcommit-priority 这个队列中!

     [NSThread detachNewThreadSelector:@selector(onPlay:) toTarget:self withObject:nil];

     - (void)onPlay
     {
          NSLog(@"1");
          dispatch_async(dispatch_get_current_queue(), ^{
               NSLog(@"2");
          }); 
     }

两句NSLog将不在同一个queue中打印!
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值