Last active
January 9, 2017 17:47
-
-
Save chriswebb09/3a3ba23641b3f1452951727a2f9af914 to your computer and use it in GitHub Desktop.
OperationQueue
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
func downloadImage(with completion: @escaping (Bool) -> ()) { | |
let queue = OperationQueue() | |
queue.name = "Image Download queue" | |
queue.maxConcurrentOperationCount = 2 | |
queue.qualityOfService = .userInteractive | |
self.downloadImage.state = .downloaded | |
queue.addOperation { () -> Void in | |
let downloader = DownloadOp(downloadImage: self.downloadImage) | |
downloader.downloadImage(url:URL(string:"http://i.imgur.com/5ac1apZ.jpg")! , handler: { image in | |
OperationQueue.main.addOperation({ | |
print("done") | |
self.imageView.image = image | |
}) | |
}) | |
} | |
completion(true) | |
} | |
func downloadButtonTapped() { | |
switch (downloadImage.state) { | |
case .preparing: | |
downloadImage(with: { result in | |
print(result) | |
dump(self.downloadImage.image) | |
}) | |
case .downloading: | |
print("nope") | |
case .downloaded: | |
print("already downloaded") | |
} | |
} |
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
import UIKit | |
class DownloadImage { | |
var image: UIImage? | |
var state = ImageState.preparing | |
} | |
enum ImageState { | |
case preparing, downloading, downloaded | |
} | |
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
import Foundation | |
class DownloadOp: Operation { | |
let downloadImage: DownloadImage | |
let session = URLSession(configuration: URLSessionConfiguration.default) | |
private var _ready = false { | |
willSet { | |
willChangeValue(forKey: "isReady") | |
} | |
didSet { | |
didChangeValue(forKey: "isReady") | |
} | |
} | |
override var isReady: Bool { | |
return _ready | |
} | |
private var _executing = false { | |
willSet { | |
willChangeValue(forKey: "isExecuting") | |
} | |
didSet { | |
didChangeValue(forKey: "isExecuting") | |
} | |
} | |
override var isExecuting: Bool { | |
return _executing | |
} | |
private var _finished = false { | |
willSet { | |
willChangeValue(forKey: "isFinished") | |
} | |
didSet { | |
didChangeValue(forKey: "isFinished") | |
} | |
} | |
override var isFinished: Bool { | |
return _finished | |
} | |
override func start() { | |
_ready = true | |
super.start() | |
_executing = true | |
execute() | |
} | |
func execute() { | |
_ready = false | |
downloadImage(url: URL(string:"http://i.imgur.com/5ac1apZ.jpg")!, handler: { image in | |
OperationQueue.main.addOperation({ | |
self.downloadImage.image = image | |
}) | |
}) | |
} | |
func finish() { | |
_executing = false | |
_finished = true | |
_ready = true | |
} | |
init(downloadImage: DownloadImage) { | |
_isReady = true | |
_isExecuting = false | |
_isFinished = false | |
_isCancelled = false | |
self.downloadImage = downloadImage | |
super.init() | |
} | |
override func main() { | |
downloadImage(url: URL(string:"http://i.imgur.com/5ac1apZ.jpg")!, handler: { image in | |
OperationQueue.main.addOperation({ | |
self.downloadImage.image = image | |
}) | |
}) | |
} | |
func getDataFromUrl(url: URL, completion: @escaping (_ data: Data?, _ response: URLResponse?, _ error: Error?) -> Void) { | |
let urlRequest = URLRequest(url:url) | |
session.dataTask(with: urlRequest, completionHandler: { data, response, error in | |
completion(data, response, error) | |
}).resume() | |
} | |
func downloadImage(url: URL, handler: @escaping (UIImage) -> Void) { | |
print("Download Started") | |
getDataFromUrl(url: url) { (data, response, error) in | |
guard let data = data, error == nil else { return } | |
OperationQueue.main.addOperation({ | |
handler(UIImage(data:data)!) | |
}) | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment