Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

main.async vs main.sync() vs global().async in Swift3 GCD

Example A:- This cause App Crash.

DispatchQueue.main.async {           
        let url = URL(string: imageUrl)
        do {
             let data = try Data(contentsOf: url!)
                DispatchQueue.main.sync {
                    self.imageIcon.image = UIImage(data: data)
                }
            }

Example B:- But This don't

DispatchQueue.global().async {  
        let url = URL(string: imageUrl)
        do {
            let data = try Data(contentsOf: url!)
                DispatchQueue.main.sync {
                    self.imageIcon.image = UIImage(data: data)
                }
            }

As per my knowledge,

  • x.sync means doing thing in main thread/UI thread and x.async means doing in background thread.
  • Global means performing something with concurrent queue i.e Parallel task.

Quest1:- So why does my app crashed when i performed task in background thread i.e main.async and than call main thread to update UI.

Quest2:- Is there any difference in main.async & global().async.

like image 563
Shivam Srivastava Avatar asked Oct 13 '17 14:10

Shivam Srivastava


People also ask

What is synchronous vs asynchronous in GCD?

parallel execution. Synchronous basically means that you can only execute one thing at a time. Asynchronous means that you can execute multiple things at a time and you don't have to finish executing the current thing in order to move on to next one.

What is DispatchQueue Global () async?

There are background threads for heavy tasks. DispatchQueue. global() runs these kind of tasks in background threads. You can tell the queue about how important your task is, so that DispatchQueue can prioritize your task. You can do this by providing Quality-of-Service information.

Is DispatchQueue main serial?

DispatchQueue. main (the main queue) is a serial queue. sync and async do not determine serialization or currency of a queue, but instead refer to how the task is handled. Synchronous function returns the control on the current queue only after task is finished.

What is main queue in iOS?

The main queue is the dispatch queue in which all the UI updates take place and the code involving UI changes are placed. You need to get to the main queue in order to update UI on completion of an asynchronous process like NSURLSession.


1 Answers

In simple term i come to conclusion that -

  • Queue- There are 3 Types of Queue i.e. 1 Main Queue, 4 Global Queue and Any No. of Custom Queues.
  • Threads- One is Main Thread and other background threads which system provides to us.

DispatchQueue.main.async

-It means performing task in main queue with using of background thread (w/o blocking of UI) and when task finish it automatic Updated to UI because its already in Main Queue.

DispatchQueue.global().async along with global().sync

It means performing task in Global Queue with using of background thread and when task finish, than global().sync use bring the work from globalQueue to mainQueue which update to UI.

Reason of My App Crash

I was trying to bring the completed task to MainQueue by using(main.sync), but it was already on MainQueue because i hadnt switched the Queue, and this create DeadLock (MainQueue waiting for itself), causes my app crash

like image 146
Shivam Srivastava Avatar answered Sep 25 '22 01:09

Shivam Srivastava