Created
January 23, 2018 12:43
-
-
Save 15458434/7d7bf48dad0455a73ad93acb00e07812 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import Foundation | |
@objc class FinalOperationSynchronizer: NSObject { | |
@objc private(set) var operations: [Operation] | |
@objc private(set) var finalOperation: Operation | |
@objc private(set) var cancelOperation: Operation? | |
private(set) var queue: OperationQueue? | |
@objc init(with operations: [Operation], and finalOperation: Operation, with cancelOperation: Operation) { | |
operations.forEach { (operation) in | |
finalOperation.addDependency(operation) | |
} | |
self.operations = operations | |
self.finalOperation = finalOperation | |
self.cancelOperation = cancelOperation | |
super.init() | |
} | |
@objc func prepareQueue() { | |
queue = OperationQueue() | |
queue?.qualityOfService = .default | |
} | |
@objc func executeFinalOperationWhenOperationsReady() { | |
if queue == nil { | |
prepareQueue() | |
} | |
queue!.addOperation(finalOperation) | |
} | |
@objc func cancelAllOperations() { | |
queue?.cancelAllOperations() | |
} | |
@objc func cancelAllOperationsAndExecuteCancelOperation() { | |
queue?.cancelAllOperations() | |
if let cancelOperation = self.cancelOperation { | |
queue?.addOperation(cancelOperation) | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Executes a FinalOperation when all other operation have been executed. Also fully compatible with Objective-C. Since I took it from an Objective-C project.