Created
June 17, 2016 00:33
-
-
Save jeremyconkin/07fbb284fa93ba44ec81800f08818dae to your computer and use it in GitHub Desktop.
This file contains hidden or 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
// | |
// Operation.swift | |
// | |
// Created by Jeremy Conkin on 1/29/16. | |
// | |
import Foundation | |
/** | |
NSOperation with convenience for setting KVO states so that operations are | |
marked complete by their queues | |
*/ | |
class Operation: NSOperation { | |
/** | |
Internal state that has a corresponding KVO property in NSOperation | |
- Ready: NSOperation.ready | |
- Executing: NSOperation.executing | |
- Finished: NSOperation.finished | |
*/ | |
enum NSOperationState : String { | |
case Ready = "isReady" | |
case Executing = "isExecuting" | |
case Finished = "isFinished" | |
} | |
/// Internal state that corresponds to the parent NSOperation state | |
var state = NSOperationState.Ready { | |
willSet { | |
willChangeValueForKey(newValue.rawValue) | |
willChangeValueForKey(state.rawValue) | |
} | |
didSet { | |
didChangeValueForKey(oldValue.rawValue) | |
didChangeValueForKey(state.rawValue) | |
} | |
} | |
// MARK: NSOperation property overrides | |
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 | |
} | |
// MARK: NSOperation method overrides | |
override func start() { | |
if cancelled { | |
state = NSOperationState.Finished | |
return | |
} | |
super.start() | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment