Skip to content

Instantly share code, notes, and snippets.

@15458434
Created January 23, 2018 12:43
Show Gist options
  • Save 15458434/7d7bf48dad0455a73ad93acb00e07812 to your computer and use it in GitHub Desktop.
Save 15458434/7d7bf48dad0455a73ad93acb00e07812 to your computer and use it in GitHub Desktop.
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)
}
}
}
@15458434
Copy link
Author

15458434 commented Jan 23, 2018

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment