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
func executeOnMain() { | |
if !Thread.isMainThread { | |
DispatchQueue.main.async(execute: {() -> Void in | |
executeOnMain() | |
}) | |
return | |
} | |
// do something | |
} |
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
OperationQueue.main |
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() |
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 |
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
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 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
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
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 GroupOperation: AsyncOperation { | |
let queue = OperationQueue() | |
var operations: [AsyncOperation] = [] | |
override func execute() { | |
print("group started") | |
queue.addOperations(operations, waitUntilFinished: true) | |
print("group done") |