Search code examples
iosswiftgrand-central-dispatchambiguousdispatch-queue

Ambiguous use of 'dispatch_get_main_queue()'


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?


Solution

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