Implementing the operation class
In this chapter so far, we have taken our long-running operations and scheduled them as blocks of code, called closures, on dispatch queues. This has made it really easy to move long-running code off of the main queue, but if we intend to reuse this long-running code, pass it around, track its state, and generally deal with it in an object-orientated way, a closure is not ideal.
To solve this, the Foundation framework provides an object, Operation
, that allows us to wrap up our block of work within an encapsulated object.
In this recipe, we will take the photo book app we used throughout this chapter and convert our long-running blocks into an Operation
instance.
Getting ready
We are going to build on the app we improved in the last recipe, which is an app that will produce a PDF photo book from a collection of photos. You can get the code for this app at https://github.com/PacktPublishing/Swift-Cookbook-Third-Edition/tree/main/Chapter%206...