Created
June 12, 2020 09:10
-
-
Save stevencurtis/71b27522cd3e1452301751fc006160bd to your computer and use it in GitHub Desktop.
networkoperation
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
class NetworkOperation: Operation { | |
typealias OperationCompletionHandler = (_ result: Result<Data, Error>) -> Void | |
/// The completionHandler that is run when the operation is complete | |
var completionHandler: (OperationCompletionHandler)? | |
/// Stte stored as an enum | |
private enum State: String { | |
case ready = "isReady" | |
case executing = "isExecuting" | |
case finished = "isFinished" | |
} | |
private var state = State.ready { | |
willSet { | |
willChangeValue(forKey: newValue.rawValue) | |
willChangeValue(forKey: state.rawValue) | |
} | |
didSet { | |
didChangeValue(forKey: oldValue.rawValue) | |
didChangeValue(forKey: state.rawValue) | |
} | |
} | |
override var isReady: Bool { | |
return super.isReady && state == .ready | |
} | |
override var isExecuting: Bool { | |
return state == .executing | |
} | |
override var isFinished: Bool { | |
return state == .finished | |
} | |
/// Start the NSOperation | |
override func start() { | |
guard !isCancelled else { | |
finish() | |
return | |
} | |
if !isExecuting { | |
state = .executing | |
} | |
main() | |
} | |
/// Move to the finished state | |
func finish() { | |
if isExecuting { | |
state = .finished | |
} | |
} | |
/// Called to indicate that the operation is complete, and then call the opional completion handler | |
/// - Parameter result: The result type | |
func complete(result: Result<Data, Error>) { | |
finish() | |
if !isCancelled { | |
completionHandler?(result) | |
} | |
} | |
/// Cancels the Operation | |
override func cancel() { | |
super.cancel() | |
finish() | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment