dispatch_get_current_queue的廢棄

由於iOS7以後 dispatch_get_current_queue 被廢棄

  • 在主線程分離出一個子線程:
 - (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event{
    [NSThread detachNewThreadSelector:@selector(onPlay) toTarget:self withObject:nil];
}
  • 接下來我們來看一下這種方式開啓線程創建出來的隊列是什麼
 - (void)onPlay
     {
         dispatch_queue_t dispatch_queue = dispatch_get_current_queue();
         NSLog(@"%s", dispatch_queue_get_label(dispatch_queue));
     }

其結果是:com.apple.root.default-overcommit-priority。

由此可見:

主線程

只有一個,並且存在一個與該線程綁定的 dispatch_queue,名字是 com.apple.main-thread,可以通過 dispatch_get_main_queue 得到。

dispatch_queue 線程

dispatch_queue 自己創建的線程,該線程與創建它的 dispatch_queue 綁定。

其他線程

通過NSThread,[NSObject performSelectorInBackground]等接口創建出來的線程,沒有與之綁定的 dispatch_queue。

也就是說,如果執行 dispatch_get_current_queue 的線程沒有與之綁定的隊列的時候,會返回一個 com.apple.root.default-overcommit-priority 的隊列,這個隊列跟該線程沒有關係。

因此我們可以猜測 dispatch_get_current_queue 被廢棄的原因就是這個函數不一定總是有意義的。

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章