Swift 3 GCD

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。


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:

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。


Finished the most basic things,Let us say something changed,Priority name。

What we do know is,GCD's default queue priority four:


Now,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:

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:

Create a queue

Just write DispatchQueue 's initializer to create a queue。The most simple and direct way is:

Complex way? You can specify the priority and queue category:

Then you can put the code in:


For a group,Now you can use this syntax to directly create a group:

As regards the use,Then this is:

Then,If you have multiple concurrent queue in the same group,We need them to complete then continue:

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:

Just a in line。

Published by R0uter

The non-declaration,I have written articles are original,Reproduced, please indicate the link on this page and my name。

Leave a comment

Your email address will not be published. Required fields are marked *