Reputation: 2059
I try to understand GCD and wrote this code to find out run priority:
override func viewDidLoad() {
super.viewDidLoad()
fetchImage()
print(1)
dispatch_async(dispatch_get_global_queue(QOS_CLASS_USER_INITIATED, 0)) {
print(2)
}
dispatch_async(dispatch_get_main_queue()) {
print(3)
}
dispatch_async(dispatch_get_global_queue(QOS_CLASS_USER_INTERACTIVE, 0)) {
print(5)
}
}
I got next result in the console:
1
2
5
3
So the question is:
Part 1: Why 3 is after 5 (main_queue has highest priority?)
Part 2: And why 2 is higher that 3 and 5 as well?
Thank you guys!
Upvotes: 3
Views: 6324
Reputation: 1907
The result is the order of the priorities.
QOS_CLASS_USER_INTERACTIVE
means user forground process, so highest priority.
QOS_CLASS_USER_INITIATED
means user is waiting for the result of the job, so very high priority.
And the main queue is normal work load as when user has seen everything and its job.
Upvotes: 0
Reputation: 119031
Bear in mind: this is multi-threading, on a multi-core device, writing output to a log that you don't know the thread safety and internal management of...
That said:
QOS_CLASS_USER_INTERACTIVE
~= main thread priority)Note, I say ~= because I haven't checked the exact values and it may differ slightly though I expect the priority values to match, otherwise 'interactive' wouldn't mean much...
Upvotes: 4