Skip to content

Instantly share code, notes, and snippets.

@stevencurtis
Created June 12, 2020 09:10
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 stevencurtis/71b27522cd3e1452301751fc006160bd to your computer and use it in GitHub Desktop.
Save stevencurtis/71b27522cd3e1452301751fc006160bd to your computer and use it in GitHub Desktop.
networkoperation
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