Xcode 8 Official version has been released,I want the first time my project to the Swift 3 --After all, this is a trend。
During the migration process, I encountered a lot of problems--such as Xcode provides automated migration tool is not working,I've been waiting for more than two hours,after that I give up,I chose to manually migrate--after all,,Xcode's auto-correction is also very good。
However--in the process of migrating,Xcode has been in the "Indexing",Function does not work well... ... May be due to Swift 3 In the process of evolution a number of syntax changes,It changes back and forth in many parts of the syntax-the end result is an error cannot be automatically corrected。
Ok,Eventually you have to manually modify,So here's what was changed to record the most common GCD。
DispatchQueue
First of all,Global functions dispatch no longer write for the underline style name,It becomes a more Swift-y style DispatchQueue look。
main thread
Same,You do not need to use dispatch_get_main_queue() To get the main thread,But DispatchQueue.main ,Then put on the main thread code, how do it? Just behind the main .async {} .,That is,Something like that:
1 2 3 |
DispatchQueue.main.async { [weak self] in your code runs in main thread } |
In fact,Xcode now auto-completion,Is wrong .asynchronously() 。
Another:All the tests in the current,I found all the writing in this closure of the code is not executed--please help find answers,If this is a bug ... ... All of my items or continue to use Swift 2.3 All right。
priority
Finished the most basic things,Let us say something changed,Priority name。
What we do know is,GCD's default queue priority four:
- DISPATCH_QUEUE_PRIORITY_HIGH
- DISPATCH_QUEUE_PRIORITY_DEFAULT
- DISPATCH_QUEUE_PRIORITY_LOW
- DISPATCH_QUEUE_PRIORITY_BACKGROUND
From now on,The new syntax,Changes the priority of these four unknowingly names to more meaningful names:
- .userInitialted
- .default
- .utility
- .background
of course,Their relationship which is identical to the order of:
1 2 3 4 |
DISPATCH_QUEUE_PRIORITY_HIGH: .userInitiated DISPATCH_QUEUE_PRIORITY_DEFAULT: .default DISPATCH_QUEUE_PRIORITY_LOW: .utility DISPATCH_QUEUE_PRIORITY_BACKGROUND: .background |
Gets a queue
We use DispatchQueue.global() Get a system queue,So get is the default .default Priority queue,If you want to get other priority queues,On the use of DispatchQueue.global(qos: .userInitiated) ,At last,We use .async {} To execute code:
1 2 3 |
DispatchQueue.global(qos: .userInitiated).async { //your code here } |
Create a queue
Just write DispatchQueue 's initializer to create a queue。The most simple and direct way is:
1 |
let queue = DispatchQueue(label: "myBackgroundQueue") |
Complex way? You can specify the priority and queue category:
1 |
let queue = DispatchQueue(label: "myBackgroundQueue", qos: .userInitiated, attributes: .concurrent) |
Then you can put the code in:
1 2 3 |
queue.async { print("aaa") } |
Groups
For a group,Now you can use this syntax to directly create a group:
1 |
let group = DispatchGroup() |
As regards the use,Then this is:
1 2 3 4 5 6 7 |
let group = DispatchGroup() let queue = DispatchQueue(label: "myBackgroundQueue") queue.async(group:group) { print("background working") } |
Then,If you have multiple concurrent queue in the same group,We need them to complete then continue:
1 |
group.wait() |
After the specified time to run
Sometimes you may need to make some code executes after the specified time,Such as after the animation is completed。The task in swift 2.3 Lot of trouble,but,3.0 is not the same:
1 2 3 |
DispatchQueue.main.asyncAfter(deadline: DispatchTime.now() + 3.0) { print("after!") } |
Just a in line。
Original article written by LogStudio:R0uter's Blog » Swift 3 GCD
Reproduced Please keep the source and description link:https://www.logcg.com/archives/2040.html