Operations
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 to Operations
.
Getting ready
You can get the code for our photo book app by visiting: https://github.com/SwiftProgrammingCookbook/PhotobookCreator, and choosing the start-operations
branch, or you can use git:
git clone https://github.com/SwiftProgrammingCookbook/PhotobookCreator.gitgit checkout start-operations
Open the project in Xcode, and navigate...