Created
April 12, 2017 15:56
-
-
Save eugeniobaglieri/a5dd1a25d297685b1666d6c75ec4f7a8 to your computer and use it in GitHub Desktop.
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
// | |
// 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