Using dispatch queues in your application

A basic understanding of threads is good enough for you to start using them in your applications. However, once you start using them, there's a good chance that they suddenly become confusing again. If this happens to you, don't worry; threading is not easy. Now let's look at an example of threaded code:

var someBoolean = false

DispatchQueue(label: "MutateSomeBoolean").async {
  // perform some work here
  for i in 0..<100 {
      continue
  }

  someBoolean = true
}

print(someBoolean)

The preceding snippet demonstrates how you could mutate a variable after performing a task that is too slow to execute on the main thread. In the preceding code, an instance of DispatchQueue is created, and it's given a label. This ...

Get Mastering iOS 12 Programming - Third Edition now with O’Reilly online learning.

O’Reilly members experience live online training, plus books, videos, and digital content from 200+ publishers.