Created
October 6, 2015 18:25
-
-
Save suparngp/1d34eb378d19fab35d1c to your computer and use it in GitHub Desktop.
Checking for states in the NSOperation
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 | |
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