0

What is point of usage

DispatchQueue.main.async {
    self.imageView.image = imageView
    self.lbltitle.text = ""
} 

or

DispatchQueue.main.sync {
    self.imageView.image = imageView
    self.lbltitle.text = ""
}

how code can run main.async if current queue is main? if main queue is serial, and there is only one main thread.

Tasks can be performed synchronously or asynchronously.

Synchronous function returns control to the current queue only after the task is finished. It blocks the queue and waits until the task is finished.

Asynchronous function returns control to the current queue(we are on main queue) right after task has been sent to be performed on the different queue(what is "different queue" in this case?). It doesn't wait until the task is finished. It doesn't block the queue.

Wouldn't it be more accurate to say that you should never call the sync() function on the current queue? It's not wrong to call sync() on the main queue if you're in another queue, if I understand correctly.

Alisher Baigazin
  • 438
  • 1
  • 3
  • 10
  • 1
    Well, main DispatchQueue is still a DispatchQueue and therefore implements the same protocol. Its up to you, as developer to use these methods or not. Sometimes you may want to queue tasks to main queue when running on some background queue. If you are already on main, sync is equivalent to a direct function call and async will execute your code sometime soon, without blocking the rest of your code, somewhat like "defer" keyword. – Dima G Oct 18 '22 at 11:51
  • I am bit confused where is the issue? Main queue is serial, right, there could be N number of tasks pushed into it, if you want to push one more push it in async manner of execution so that Main queue will keep doing what's its doing and it will attend to your task when the time comes. If you push it with sync - that would stop main queue from whatever it was doing, block it, and take up your task first(provided other tasks pushed before you were with async). – Saket Kumar Oct 18 '22 at 12:10
  • 1
    @SaketKumar question is, what will happen if i push task async/sync to main, when I am already on main queue. But I found the answer, yeah you right on async behavior, but when we push sync, running DispatchQueue.main.sync from the main queue and the app will freeze because the calling queue will wait until the dispatched block is over but it won't be even able to start (because the queue is stopped and waiting) – Alisher Baigazin Oct 18 '22 at 12:26
  • 2
    `sync` call while being on the same serial queue will deadlock forever. `async` call should bounce the work item until the next runloop. – pronebird Oct 18 '22 at 12:33
  • 1
    “It's not wrong to call `sync()` on the main queue if you're in another queue, if I understand correctly.” ... Technically correct, but I would advise avoiding using `sync` unless you absolutely need to have the current thread block while it waits for the dispatched block to run on the main thread. And if you must use `sync`, make sure that you never synchronously dispatch from a thread back to itself or else that will deadlock. But if you are just updating the UI, there is no reason to block the background thread to wait for the main thread to update the UI. Use `async` in these cases. – Rob Oct 18 '22 at 17:41

0 Answers0