Skip to content

Instantly share code, notes, and snippets.

@alexisakers
Forked from calebd/AsynchronousOperation.swift
Last active July 18, 2017 03:35
Show Gist options
  • Star 3 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save alexisakers/1ee81a952b11a2ddc6a43480cc59032c to your computer and use it in GitHub Desktop.
Save alexisakers/1ee81a952b11a2ddc6a43480cc59032c to your computer and use it in GitHub Desktop.
Concurrent Operation in Swift 3
//
// ConcurrentOperation.swift
//
// Created by Caleb Davenport on 7/7/14.
//
// Learn more at http://blog.calebd.me/swift-concurrent-operations
//
import Foundation
class ConcurrentOperation: Operation {
// MARK: - Types
enum State {
case ready, executing, finished
var keyPath: String {
switch self {
case .ready:
return "isReady"
case .executing:
return "isExecuting"
case .finished:
return "isFinished"
}
}
}
// MARK: - Properties
var state = State.ready {
willSet {
willChangeValue(forKey: newValue.keyPath)
willChangeValue(forKey: state.keyPath)
}
didSet {
didChangeValue(forKey: oldValue.keyPath)
didChangeValue(forKey: state.keyPath)
}
}
// MARK: - NSOperation
override var isReady: Bool {
return super.isReady && state == .ready
}
override var isExecuting: Bool {
return state == .executing
}
override var isFinished: Bool {
return state == .finished
}
override var isAsynchronous: Bool {
return true
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment