Forked from calebd/AsynchronousOperation.swift
Last active
July 18, 2017 03:35
Concurrent Operation in Swift 3
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
// | |
// 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