One thing I can't fully understand, why this code don't cause a deadlock:
let queue = DispatchQueue(label: "queue_label")
print("\(Thread.current)") // <_NSMainThread: 0x60000051c440>{number = 1, name = main}
queue.sync {
self.view.backgroundColor = .red // ok
print("in sync \(Thread.current)") // <_NSMainThread: 0x60000051c440>{number = 1, name = main}
}
sync task performed by serial main thread - we see it by print statement and we can additionally prove it by successfully change UI (backgroundColor).
If we change queue.sync to DisptachQueue.main.sync - we of course get a deadlock. What's the difference here?
Topic:
App & System Services
SubTopic:
Processes & Concurrency
Tags: