Schedulers in practice

Let's now work with a code example to see schedulers in action. We will start out in a playground in which we will import PlaygroundSupport and set needsIndefiniteExecution to true, because we will write concurrent code. We have also added two images to the resources folder for the playground page.

We have displayed the assistant editor, which you can enable by navigating to View | Assistant Editor | Show Assistant Editor, as shown in the following screenshot:

If you look at the setup project, you will note that we have some setup code in the playground:

import PlaygroundSupport
PlaygroundPage.current.needsIndefiniteExecution = true
import RxSwift
let imageView = UIImageView(
frame: CGRect(
x: 0,
y: 0,
width: 128,
height: 128
)
)
PlaygroundPage.current.liveView = imageView

This code creates an imageView and then sets it to be the live view for the playground page. We have also created image data for the Swift and Rx images that we added to the resources folder as follows:

let swift = UIImage(named: "Swift")!
let swiftImageData = UIImagePNGRepresentation(swift)!
let rx = UIImage(named: "Rx")!
let rxImageData = UIImagePNGRepresentation(rx)!
let disposeBag = DisposeBag()

Imagine that this image data was retrieved via a URLSessionRequest, and you can see that we have added a disposeBag as well.

We will demonstrate how to push the work of creating UIImages from data onto a background thread.

Just a side note: If this data were coming from a URLSession request, it would already be on a background queue.