Last active
January 23, 2017 11:21
-
-
Save ianpartridge/e3b8496cb114005a93f5c86ff76af630 to your computer and use it in GitHub Desktop.
Asynchronous Operation in Swift 3
This file contains 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 | |
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