Skip to content

Instantly share code, notes, and snippets.

@vani2
Created November 12, 2020 16:01
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save vani2/12d4e40e3a476042c28c405ccc84dc96 to your computer and use it in GitHub Desktop.
Save vani2/12d4e40e3a476042c28c405ccc84dc96 to your computer and use it in GitHub Desktop.
class AsyncOperation: Operation {
enum State: String {
case ready, executing, finished
fileprivate var keyPath: String {
return "is" + rawValue.capitalized
}
}
var state = State.ready {
willSet {
willChangeValue(forKey: state.keyPath)
willChangeValue(forKey: newValue.keyPath)
}
didSet {
didChangeValue(forKey: state.keyPath)
didChangeValue(forKey: oldValue.keyPath)
}
}
override var isAsynchronous: Bool {
return true
}
override var isReady: Bool {
return super.isReady && state == .ready
}
override var isExecuting: Bool {
return state == .executing
}
override var isFinished: Bool {
return state == .finished
}
override func start() {
if isCancelled {
state = .finished
} else {
main()
state = .executing
}
}
override func cancel() {
super.cancel()
state = .finished
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment