13

How do I replace the following Swift code for iOS using the DispatchQueue class? This is old Swift 3 code that the newest Xcode won't convert to Swift 5.

dispatch_async(dispatch_get_main_queue()) { () -> Void in
  // code
}

It is giving me an error that says

Ambiguous use of 'dispatch_get_main_queue()'

for dispatch_get_main_queue().

The following answer seems to be the right answer, and I want to use it, but could someone tell me this is correct? Swift version

It says to use the following code:

DispatchQueue.global(qos: .background).async {

    // Background Thread

    DispatchQueue.main.async {
        // Run UI Updates
    }
}

Other than using DispatchQueue, what other alternative do I have to fix this error?

Rob
  • 415,655
  • 72
  • 787
  • 1,044
daniel
  • 1,446
  • 3
  • 29
  • 65
  • 1
    Not that it’s terribly relevant, but this `dispatch_sync` code is likely older than that, predating Swift 3, in which this new `DispatchQueue` syntax was introduced. See WWDC 2016’s [Concurrent Programming With GCD in Swift 3](https://developer.apple.com/videos/play/wwdc2016/720/). – Rob Apr 20 '19 at 21:57

1 Answers1

6

Yes, it is correct. That's the new updated syntax.

Tom Pearson
  • 384
  • 1
  • 8