How to correctly use dispatch_sync, dispatch_async, dispatch_after and other GCD functions in Swift 3, 4 and newer?
Hello everyone! I'm transitioning from Objective-C to Swift, and I'm confused about Grand Central Dispatch (GCD) after the release of Swift 3. Previously I wrote something like:
```swift
dispatch_async(dispatch_get_main_queue(), {
// UI update
})
```
But now Xcode says this is incorrect. I understand that Apple changed the API, but I can't find a clear explanation of how to work with GCD in Swift 3, 4 and newer. I need to understand:
1. How to execute code asynchronously on the main queue (for UI updates)?
2. How to execute code synchronously (dispatch_sync) on a background queue?
3. How to add a delay before execution (dispatch_after)?
4. How to create my own queue?
I tried writing like this, but it doesn't work:
```swift
DispatchQueue.main.async {
// code
}
DispatchQueue.globa
No answers yet.
Log in to leave an answer