(5.) 新增一個finish() function 來定義Asynchronous Operation何時結束
(6.) 繼承FCAsynchronousOperation的subclass需implement main() function
importUIKitpublicclassFCAsynchronousOperation:Operation {@objcprivateenumState:Int{case readycase executingcase finished }/// Concurrent queue for synchronizing access to `state`.privatelet stateQueue =DispatchQueue(label: Bundle.main.bundleIdentifier!+".operationQueue.state", attributes: .concurrent)/// Private backing stored property for `state`.privatevar _state: State = .ready/// The state of the operation@objcprivatedynamicvar state: State {get { return stateQueue.sync { _state } }set { stateQueue.sync(flags: .barrier) { _state = newValue } } }// MARK: - Various `Operation` propertiesopenoverridevar isAsynchronous: Bool { returntrue }openoverridevar isReady: Bool { return state == .ready && super.isReady }publicfinaloverridevar isExecuting: Bool { return state == .executing }publicfinaloverridevar isFinished: Bool { return state == .finished }// KVN for dependent propertiesopenoverrideclassfunckeyPathsForValuesAffectingValue(forKeykey: String) ->Set<String> {if["isReady", "isFinished", "isExecuting"].contains(key) {return[#keyPath(state)] }return super.keyPathsForValuesAffectingValue(forKey: key) }// Startpublicfinaloverridefuncstart() {if isCancelled {finish()return } state = .executingmain() }/// Subclasses must implement this to perform their work and they must not call `super`. The default implementation of this function throws an exception.openoverridefuncmain() {fatalError("Subclasses must implement `main`.") }/// Call this function to finish an operation that is currently executingpublicfinalfuncfinish() {if isExecuting { state = .finished } }}