Skip to content

Instantly share code, notes, and snippets.

@jeremyconkin
Created June 17, 2016 00:33
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 jeremyconkin/07fbb284fa93ba44ec81800f08818dae to your computer and use it in GitHub Desktop.
Save jeremyconkin/07fbb284fa93ba44ec81800f08818dae to your computer and use it in GitHub Desktop.
//
// 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