Skip to content

Instantly share code, notes, and snippets.

@malcommac
Last active September 9, 2018 09:47
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 malcommac/7b441d012761e7c230ced380591cef94 to your computer and use it in GitHub Desktop.
Save malcommac/7b441d012761e7c230ced380591cef94 to your computer and use it in GitHub Desktop.
ImageFetcherOperation.swift
import UIKit
internal class ImageSizeFetcherOp: Operation {
/// Callback to call at the end of the operation
let callback: ImageSizeFetcher.Callback?
/// Request data task
let request: URLSessionDataTask
/// Partial data
private(set) var receivedData = Data()
/// URL of the operation
var url: URL? {
return self.request.currentRequest?.url
}
/// Initialize a new operation for a given url.
///
/// - Parameters:
/// - request: request to perform.
/// - callback: callback to call at the end of the operation.
init(_ request: URLSessionDataTask, callback: ImageSizeFetcher.Callback?) {
self.request = request
self.callback = callback
}
///MARK: - Operation Override Methods
override func start() {
guard !self.isCancelled else { return }
self.request.resume()
}
override func cancel() {
self.request.cancel()
super.cancel()
}
//MARK: - Internal Helper Methods
func onReceiveData(_ data: Data) {
guard !self.isCancelled else { return }
self.receivedData.append(data)
// not enough data collected for anything
guard data.count >= 2 else { return }
// attempt to parse received data, if enough we can stop download
do {
if let result = try ImageSizeFetcherParser(sourceURL: self.url!, data) {
self.callback?(nil,result)
self.cancel()
}
// nothing received, continue accumulating data
} catch let err { // parse has failed
self.callback?(err,nil)
self.cancel()
}
}
func onEndWithError(_ error: Error?) {
// download has failed, return to callback with the description of the error
self.callback?(ImageParserErrors.network(error),nil)
self.cancel()
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment