Using Operation subclassing
Most often, operations that you need to perform are better encapsulated in a custom subclass of the Operation
class. We have already worked with BlockOperation
in the previous demo, but we saw a lot of redundancy in writing code and it's not customized. In this section, we will implement the same demo and see how we build a custom Operation
class to perform the task that will be done concurrently.
How to do it...
To build a custom Operation
class, perform the following steps:
In our Xcode project, add a new Swift file with a class, named
ImageDownloader
, which extends theOperation
class:In the
ImageDownloader.swift
file, add the following code:class ImageDownloader: Operation { let imgURL: URL var downloadedImage: UIImage? init(imageURL: URL) { self.imgURL = imageURL } override func main() { if self.isCancelled { ...