Search code examples
iosswiftconcurrencydispatch-queue

Why a sync block of code always call on main thread?


I did the simple test with DispatchQueue:

DispatchQueue.global(qos: .background).sync {
  if Thread.isMainThread {
    print("Main thread")
  }
}

It printed out:

Main thread

Why does this code execute on the main thread? It should be performed on a background thread (it was added to a background queue), right?


Solution

  • Because it doesn't actually have to. You're blocking the main thread by using sync. iOS is choosing to just execute it on the main thread instead of bothering to switch to a background thread (of a background queue) as it doesn't really matter due to main thread being blocked anyways.

    Apple's docs (and quickhelp) on the sync function include the line:

    As an optimization, this function invokes the block on the current thread when possible.