Swift3 GCD队列优先级说明
从ios8开始,苹果引入了一个新的概念 QoS(quality of service),用于指定GCD队列的优先级。
swift3之前:只有4个优先级
high > default > low > background
swift3之后:调整为6个优先级
userInteractive > default > unspecified > userInitiated > utility > background
其中:userInteractive优先级最高,background最低
现有和之前的大概对应关系:
* DISPATCH_QUEUE_PRIORITY_HIGH: .userInteractive * DISPATCH_QUEUE_PRIORITY_DEFAULT: .default * DISPATCH_QUEUE_PRIORITY_LOW: .utility * DISPATCH_QUEUE_PRIORITY_BACKGROUND: .background
我们使用时:
系统提供两个方法:
1、使用global(),系统会默认给予Default优先级
2、使用global(qos:xxx)自定义自己想要的优先级
测试代码:(摘自网络,侵权删)
1、我们对每个优先级进行打印:
for i in 1...10 { DispatchQueue.global(qos: DispatchQoS.QoSClass.default).async { NSLog("DispatchQoS.QoSClass.default, %d", i) } DispatchQueue.global(qos: DispatchQoS.QoSClass.background).async { NSLog("DispatchQoS.QoSClass.background, %d", i) } DispatchQueue.global(qos: DispatchQoS.QoSClass.unspecified).async { NSLog("DispatchQoS.QoSClass.unspecified, %d", i) } DispatchQueue.global(qos: DispatchQoS.QoSClass.userInitiated).async { NSLog("DispatchQoS.QoSClass.userInitiated, %d", i) } DispatchQueue.global(qos: DispatchQoS.QoSClass.userInteractive).async { NSLog("DispatchQoS.QoSClass.userInteractive, %d", i) } DispatchQueue.global(qos: DispatchQoS.QoSClass.utility).async { NSLog("DispatchQoS.QoSClass.utility, %d", i) } }
打印结果:
2016-11-15 11:46:12.927070 GraphicsContext[2099:635166] DispatchQoS.QoSClass.userInteractive, 1 2016-11-15 11:46:12.927363 GraphicsContext[2099:635166] DispatchQoS.QoSClass.userInteractive, 2 2016-11-15 11:46:12.927407 GraphicsContext[2099:635171] DispatchQoS.QoSClass.userInteractive, 3 2016-11-15 11:46:12.927490 GraphicsContext[2099:635166] DispatchQoS.QoSClass.userInteractive, 4 2016-11-15 11:46:12.927674 GraphicsContext[2099:635166] DispatchQoS.QoSClass.userInteractive, 5 2016-11-15 11:46:12.927780 GraphicsContext[2099:635166] DispatchQoS.QoSClass.userInteractive, 6 2016-11-15 11:46:12.927868 GraphicsContext[2099:635172] DispatchQoS.QoSClass.userInteractive, 7 2016-11-15 11:46:12.927871 GraphicsContext[2099:635166] DispatchQoS.QoSClass.userInteractive, 8 2016-11-15 11:46:12.928016 GraphicsContext[2099:635172] DispatchQoS.QoSClass.userInteractive, 9 2016-11-15 11:46:12.928138 GraphicsContext[2099:635166] DispatchQoS.QoSClass.userInteractive, 10 2016-11-15 11:46:12.928719 GraphicsContext[2099:635113] DispatchQoS.QoSClass.default, 1 2016-11-15 11:46:12.928817 GraphicsContext[2099:635166] DispatchQoS.QoSClass.unspecified, 1 2016-11-15 11:46:12.928924 GraphicsContext[2099:635172] DispatchQoS.QoSClass.userInitiated, 1 2016-11-15 11:46:12.929190 GraphicsContext[2099:635173] DispatchQoS.QoSClass.default, 2 2016-11-15 11:46:12.929225 GraphicsContext[2099:635171] DispatchQoS.QoSClass.utility, 1 2016-11-15 11:46:12.929723 GraphicsContext[2099:635174] DispatchQoS.QoSClass.unspecified, 2 2016-11-15 11:46:12.930104 GraphicsContext[2099:635176] DispatchQoS.QoSClass.userInitiated, 2 2016-11-15 11:46:12.930131 GraphicsContext[2099:635175] DispatchQoS.QoSClass.utility, 2 2016-11-15 11:46:12.930641 GraphicsContext[2099:635177] DispatchQoS.QoSClass.default, 3 2016-11-15 11:46:12.931705 GraphicsContext[2099:635179] DispatchQoS.QoSClass.unspecified, 3 2016-11-15 11:46:12.931857 GraphicsContext[2099:635180] DispatchQoS.QoSClass.userInitiated, 3 2016-11-15 11:46:12.932196 GraphicsContext[2099:635181] DispatchQoS.QoSClass.default, 4 2016-11-15 11:46:12.932277 GraphicsContext[2099:635178] DispatchQoS.QoSClass.utility, 3 2016-11-15 11:46:12.932779 GraphicsContext[2099:635183] DispatchQoS.QoSClass.unspecified, 4 2016-11-15 11:46:12.932889 GraphicsContext[2099:635182] DispatchQoS.QoSClass.userInitiated, 4 2016-11-15 11:46:12.933215 GraphicsContext[2099:635185] DispatchQoS.QoSClass.default, 5 2016-11-15 11:46:12.933027 GraphicsContext[2099:635184] DispatchQoS.QoSClass.utility, 4 2016-11-15 11:46:12.933423 GraphicsContext[2099:635113] DispatchQoS.QoSClass.unspecified, 5 2016-11-15 11:46:12.933586 GraphicsContext[2099:635166] DispatchQoS.QoSClass.userInitiated, 5 2016-11-15 11:46:12.933709 GraphicsContext[2099:635172] DispatchQoS.QoSClass.default, 6 2016-11-15 11:46:12.933831 GraphicsContext[2099:635173] DispatchQoS.QoSClass.unspecified, 6 2016-11-15 11:46:12.933976 GraphicsContext[2099:635174] DispatchQoS.QoSClass.userInitiated, 6 2016-11-15 11:46:12.934095 GraphicsContext[2099:635176] DispatchQoS.QoSClass.default, 7 2016-11-15 11:46:12.934242 GraphicsContext[2099:635177] DispatchQoS.QoSClass.unspecified, 7 2016-11-15 11:46:12.934356 GraphicsContext[2099:635179] DispatchQoS.QoSClass.userInitiated, 7 2016-11-15 11:46:12.934503 GraphicsContext[2099:635180] DispatchQoS.QoSClass.default, 8 2016-11-15 11:46:12.934611 GraphicsContext[2099:635171] DispatchQoS.QoSClass.utility, 5 2016-11-15 11:46:12.934701 GraphicsContext[2099:635175] DispatchQoS.QoSClass.utility, 6 2016-11-15 11:46:12.937166 GraphicsContext[2099:635186] DispatchQoS.QoSClass.unspecified, 8 2016-11-15 11:46:12.937476 GraphicsContext[2099:635188] DispatchQoS.QoSClass.userInitiated, 8 2016-11-15 11:46:12.937521 GraphicsContext[2099:635187] DispatchQoS.QoSClass.utility, 7 2016-11-15 11:46:12.937682 GraphicsContext[2099:635181] DispatchQoS.QoSClass.default, 9 2016-11-15 11:46:12.937868 GraphicsContext[2099:635183] DispatchQoS.QoSClass.unspecified, 9 2016-11-15 11:46:12.937987 GraphicsContext[2099:635182] DispatchQoS.QoSClass.userInitiated, 9 2016-11-15 11:46:12.938110 GraphicsContext[2099:635185] DispatchQoS.QoSClass.default, 10 2016-11-15 11:46:12.938262 GraphicsContext[2099:635113] DispatchQoS.QoSClass.unspecified, 10 2016-11-15 11:46:12.938381 GraphicsContext[2099:635166] DispatchQoS.QoSClass.userInitiated, 10 2016-11-15 11:46:12.938692 GraphicsContext[2099:635178] DispatchQoS.QoSClass.utility, 8 2016-11-15 11:46:12.938837 GraphicsContext[2099:635184] DispatchQoS.QoSClass.utility, 9 2016-11-15 11:46:12.941483 GraphicsContext[2099:635174] DispatchQoS.QoSClass.utility, 10 2016-11-15 11:46:12.949181 GraphicsContext[2099:635115] DispatchQoS.QoSClass.background, 1 2016-11-15 11:46:12.949734 GraphicsContext[2099:635115] DispatchQoS.QoSClass.background, 2 2016-11-15 11:46:12.950257 GraphicsContext[2099:635115] DispatchQoS.QoSClass.background, 3 2016-11-15 11:46:12.950775 GraphicsContext[2099:635115] DispatchQoS.QoSClass.background, 4 2016-11-15 11:46:12.951348 GraphicsContext[2099:635115] DispatchQoS.QoSClass.background, 5 2016-11-15 11:46:12.952512 GraphicsContext[2099:635115] DispatchQoS.QoSClass.background, 6 2016-11-15 11:46:12.953108 GraphicsContext[2099:635115] DispatchQoS.QoSClass.background, 7 2016-11-15 11:46:12.953659 GraphicsContext[2099:635115] DispatchQoS.QoSClass.background, 8 2016-11-15 11:46:12.954176 GraphicsContext[2099:635115] DispatchQoS.QoSClass.background, 9 2016-11-15 11:46:12.954890 GraphicsContext[2099:635115] DispatchQoS.QoSClass.background, 10
结果分析:
其中:
1、userinteractive 10个全部第一轮就打印出来,优先级最高。
2、background 10个全部最后才打印出来,优先级最低。
3、中间几个大致为 default > unspecified > userInitiated > utility
~