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
let op1 = Operation1() | |
let op2 = Operation2() | |
let adapter = BlockOperation() { [unowned op1, unowned op2] in | |
op2.data = op1.data | |
} | |
adapter.addDependency(op1) | |
op2.addDependency(adapter) |
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 | |
class OperationObserver: NSObject { | |
init(operation: AsyncOperation) { | |
super.init() | |
operation.addObserver(self, forKeyPath: "finished", options: .new, context: nil) | |
} | |
override func observeValue(forKeyPath keyPath: String?, of object: Any?, change: [NSKeyValueChangeKey : Any]?, context: UnsafeMutableRawPointer?) { | |
guard let key = keyPath else { |
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 | |
class HelloWorldOperation: GroupOperation { | |
override init() { | |
super.init() | |
let op = TextOperation(text: "Hello") | |
let op2 = TextOperation(text: "World") | |
op2.addDependency(op) |
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 | |
class GroupOperation: AsyncOperation { | |
let queue = OperationQueue() | |
var operations: [AsyncOperation] = [] | |
override func execute() { | |
print("group started") | |
queue.addOperations(operations, waitUntilFinished: true) | |
print("group done") |
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 | |
class TextOperation: AsyncOperation { | |
let text: String | |
init(text: String) { | |
self.text = text | |
} | |
override func execute() { |
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 | |
class AsyncOperation: Operation { | |
override var isAsynchronous: Bool { | |
return true | |
} | |
var _isFinished: Bool = false | |
override var isFinished: Bool { |
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
let op1 = Operation() | |
op.completionBlock = { | |
print("done") | |
} |
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
precedencegroup OperationChaining { | |
associativity: left | |
} | |
infix operator ==> : OperationChaining | |
@discardableResult | |
func ==><T: Operation>(lhs: T, rhs: T) -> T { | |
rhs.addDependency(lhs) | |
return rhs | |
} |
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
let op = DemoOperation() | |
OperationQueue.addOperations([op], waitUntilFinished: false) | |
op.cancel() |
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
let operationQueue: OperationQueue = OperationQueue() | |
operationQueue.maxConcurrentOperationCount = 1 |