Skip to content

Instantly share code, notes, and snippets.

@suparngp
Created October 6, 2015 18:25
Show Gist options
  • Save suparngp/1d34eb378d19fab35d1c to your computer and use it in GitHub Desktop.
Save suparngp/1d34eb378d19fab35d1c to your computer and use it in GitHub Desktop.
Checking for states in the NSOperation
import Foundation
class MyOperation:NSOperation{
// the states of the operation.
enum State {
case Ready, Executing, Finished
func keyPath() -> String {
switch self {
case Ready:
return "isReady"
case Executing:
return "isExecuting"
case Finished:
return "isFinished"
}
}
}
// MARK: - Properties
//this takes care of setting the state property with KVO
var state = State.Ready {
willSet {
willChangeValueForKey(newValue.keyPath())
willChangeValueForKey(state.keyPath())
}
didSet {
didChangeValueForKey(oldValue.keyPath())
didChangeValueForKey(state.keyPath())
}
}
// you can now easily check for the states as enums
override var ready: Bool {
return super.ready && state == .Ready
}
override var executing: Bool {
return state == .Executing
}
override var finished: Bool {
return state == .Finished
}
override var asynchronous: Bool {
return true
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment