Skip to content

Instantly share code, notes, and snippets.

@eugeniobaglieri
Created April 12, 2017 15:56
Show Gist options
  • Save eugeniobaglieri/a5dd1a25d297685b1666d6c75ec4f7a8 to your computer and use it in GitHub Desktop.
Save eugeniobaglieri/a5dd1a25d297685b1666d6c75ec4f7a8 to your computer and use it in GitHub Desktop.
//
// NetworkOperation.swift
// OperationsTest
//
// Created by Eugenio Baglieri on 11/04/17.
// Copyright © 2017 Eugenio Baglieri. All rights reserved.
//
import Foundation
public enum NetworkError: Error {
case badInput
case noData
case unacceptableResponse(Int)
}
public class NetworkOperation<Output>: Operation {
public typealias Result = () -> Output
public typealias CompletionHandler = (_ result: Result) -> Void
let internalQueue: OperationQueue
let requestOperation: RequestOperation
let completion: CompletionHandler?
init(environment: Environment, request: Request, completionHandler: CompletionHandler?) throws {
requestOperation = RequestOperation(environment: environment, request: request)
completion = CompletionHandler
internalQueue = OperationQueue()
internalQueue.delegate
internalQueue.isSuspended = true
super.init()
}
public override func execute() {
guard isCancelled == false else { finish() }
let decodeOperation = BlockOperation {
let result = decodeResponse(requestOperation.response!)
}
let finishOperation = BlockOperation {
DispatchQueue.main.async {
completion? { result }
}
}
}
public func decodeResponse(_ response: Response) -> Result {
fatalError("decodeResponse not overridden")
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment