由于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中打印!