How to use dispatch_get_main_queue swift 3?

Hello friends.

O swit 2.x I use the below code and worked ok, but I was trying use on swift 3 and I cant, I dont understand how to write it on swift 3


dispatch_async(dispatch_get_main_queue()) {
//do something
}


How can I write this code on Swift 3?

Accepted Reply

DispatchQueue.main.async {
   // do something
}

Replies

DispatchQueue.main.async {
   // do something
}

I could not understand, but it does not work. I try to Execute:

DispatchQueue.main.async {

self.refreshControl.beginRefreshing()

}


But it does not work.

Also

dispatch_async(dispatch_get_main_queue(), {

self.refreshControl.beginRefreshing()

}

in Swift 2 work perfect.

Try the following:

DispatchQueue.main.async(execute: {
    /// code goes here
})

DispatchQueue.main.async(execute: {

self.refreshControl.beginRefreshing()

})


It still do not work 😟

Does it not execute, or do you get an error?

1) No Errors

2) Code executed (I add some print("test") inside), but I think NOT in main thread, because I do not see any changes in GUI

… but I think NOT in main thread …

Add code like this to your block:

assert(Thread.isMainThread)

That’ll trap if you’re off the main thread. Conversely, if it doesn’t trap then you know the problem is something else.

Share and Enjoy

Quinn “The Eskimo!”
Apple Developer Relations, Developer Technical Support, Core OS/Hardware

let myEmail = "eskimo" + "1" + "@apple.com"

Thank you so much. I found a problem.

It happens, because I executed:

self.refreshControl.beginRefreshing()

twice. After I removed the second execution everything start works perfect. Also in Swift 2 it works without any problem.