Skip to content

Instantly share code, notes, and snippets.

@nunogoncalves
Last active January 6, 2016 14:50
Show Gist options
  • Save nunogoncalves/7f23257f73a43900a751 to your computer and use it in GitHub Desktop.
Save nunogoncalves/7f23257f73a43900a751 to your computer and use it in GitHub Desktop.
import Foundation
protocol Getter {
typealias Element
func getUrl() -> String
func getDataFrom(dictionary: NSDictionary) -> Element
func get(success success: Element -> (), failure: NetworkStatus -> ())
}
extension Getter {
func get(success success: Element -> (), failure: NetworkStatus -> ()) {
let qos = Int(QOS_CLASS_USER_INTERACTIVE.rawValue)
dispatch_async(dispatch_get_global_queue(qos, 0)) {
let responseHandler = ResponseHandler()
responseHandler.failureCallback = { status in
dispatch_async(dispatch_get_main_queue()) {
failure(status)
}
}
responseHandler.successCallback = { dictionary in
let data = self.getDataFrom(dictionary)
dispatch_async(dispatch_get_main_queue()) {
success(data)
}
}
Network.Requester(networkResponseHandler: responseHandler).makeGet(self.getUrl())
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment