O'Reilly logo

Mastering iOS 11 Programming - Second Edition by Donny Wals

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

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, chances are that they suddenly become confusing again. If this happens to you, don't worry; threading is not easy. Now, let's go ahead,  dive deep, and 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 global variable after performing a task that is too slow to execute on the main thread. We create an instance of a DispatchQueue and give it a label. This ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required