Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 228
Vuong Cuong Avatar asked Oct 23 '18 16:10

Vuong Cuong


2 Answers

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.

like image 114
Smartcat Avatar answered Nov 05 '22 11:11

Smartcat


The problem is you asked the wrong question. Don't confuse queues and threads. Work with queues and let them rejigger the threads as needed. That is exactly why we have queues! You have no business worrying what thread you're on. All you need to know is that you're on the right queue, which you can find out like this:

let q = DispatchQueue.global(qos: .background)
q.sync {
    dispatchPrecondition(condition: .onQueue(q))
    print("it's okay! we're on the right queue") // yep
}
like image 21
matt Avatar answered Nov 05 '22 10:11

matt