Sobes.tech
Junior — Middle

Under what conditions will using main.sync not lead to deadlock?

sobes.tech AI

Answer from AI

Using DispatchQueue.main.sync will not lead to a deadlock only if the call is not made from the main thread. If you call main.sync from the main thread, the thread will wait for the block to finish, which cannot start because the main thread is already busy — this causes a deadlock.

Safe usage example:

DispatchQueue.global().async {
    DispatchQueue.main.sync {
        // Update UI
    }
}

Here, the call to main.sync is made from a background thread, so no deadlock occurs. The main rule is — never call DispatchQueue.main.sync from the main thread.

Under what conditions will using main.sync not lead… - sobes.tech