Skip to content

Instantly share code, notes, and snippets.

@ianpartridge
Last active January 23, 2017 11:21
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ianpartridge/e3b8496cb114005a93f5c86ff76af630 to your computer and use it in GitHub Desktop.
Save ianpartridge/e3b8496cb114005a93f5c86ff76af630 to your computer and use it in GitHub Desktop.
Asynchronous Operation in Swift 3
import Foundation
import Dispatch
class AsyncOperation: Foundation.Operation {
private let queue = DispatchQueue(label: "async.operation.queue")
private var _executing = false
private var _finished = false
override internal(set) var isExecuting: Bool {
get {
return _executing
}
set {
if _executing != newValue {
willChangeValue(forKey: "isExecuting")
_executing = newValue
didChangeValue(forKey: "isExecuting")
}
}
}
override internal(set) var isFinished: Bool {
get {
return _finished
}
set {
if _finished != newValue {
willChangeValue(forKey: "isFinished")
_finished = newValue
didChangeValue(forKey: "isFinished")
}
}
}
override var isAsynchronous: Bool {
return true
}
override func start() {
if isCancelled {
isFinished = true
return
}
isExecuting = true
queue.async {
print("Start")
sleep(5)
print("Done")
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment