0

In my iOS app, I make a lot of web requests. When these requests succeed / fail, a delegate method in the view controller is triggered. The delegate method contains code that is responsible for updating the UI. In the following examples didUpdate(foo:) is the delegate method, and presentAlert(text:) is my UI update.

Without DispatchQueue, the code would like this:

func didUpdate(foo: Foo) {
  self.presentAlert(text: foo.text)
}

func presentAlert(text: String) {
  let alertController = ...

  self.present(alertController, animated: true)
}

When it comes to using DispatchQueue to make sure my UI will update quickly, I start to lose my ability to tell what's actually happening in the code. Is there any difference between the following two implementations?

First Way:

func didUpdate(foo: Foo) {
  self.presentAlert(text: foo.text)
}

func presentAlert(text: String) {
  let alertController = ...

  DispatchQueue.main.async {
    self.present(alertController, animated: true)
  }
}

Second way:

func didUpdate(foo: Foo) {
  DispatchQueue.main.async {
    self.presentAlert(text: foo.text)
  }
}

func presentAlert(text: String) {
  let alertController = ...

  self.present(alertController, animated: true)
}
  • Does it matter which approach I go with? It seems like having the DispatchQueue block inside of the presentAlert function is better, so I don't have to include DispatchQueue.main.async any time I want to call presentAlert?

  • Is it only necessary to explicitly send a block to the main queue when you (or a framework you are using) has "moved" yourself into a background queue?

  • If there are any external resources that may help my understanding of GCD, please let me know!

1 Answers1

1

Does it matter which approach I go with? It seems like having the DispatchQueue block inside of the presentAlert function is better, so I don't have to include DispatchQueue.main.async any time I want to call presentAlert?

There is no difference between the two approaches. But the disadvantage with the second approach, like you said, is you have to wrap all the calls to presentAlert around the DispatchQueue.main.async closure.

Is it only necessary to explicitly send a block to the main queue when you (or a framework you are using) has "moved" yourself into a background queue?

If your question here is whether there is going to be a problem if you dispatch to the main queue from the main queue, then the answer is no. If you dispatch asynchronously on the main queue from within the main queue, all it does is call your method later in the run loop.

If there are any external resources that may help my understanding of GCD, please let me know!

There are many sources on the Internet to understand GCD better. Check out this Raywenderlich tutorial. Its a good place to start.

My recommendation would be, if you have a central class that handles all the web service calls, it might be better to invoke the completion callback closure on the main queue once you parse your data after getting the web service response. This way, you won't have to keep dispatching to the main queue in your view or viewcontroller classes.

Pranay
  • 846
  • 6
  • 10