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 the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.