Skip to content

Instantly share code, notes, and snippets.

@olbrichj
Created March 21, 2018 10:48
Show Gist options
  • Save olbrichj/ea57cad595ce7434e62da67bc08e8721 to your computer and use it in GitHub Desktop.
Save olbrichj/ea57cad595ce7434e62da67bc08e8721 to your computer and use it in GitHub Desktop.
class NetworkOperation: Operation {
var targetURL: String?
var resultData: Data?
var _finished = false
override var isFinished: Bool {
get {
return _finished
}
set (newValue) {
willChangeValue(for: \.isFinished)
_finished = newValue
didChangeValue(for: \.isFinished)
}
}
convenience init(url: String) {
self.init()
targetURL = url
}
override func start() {
if isCancelled {
isFinished = true
return
}
guard let url = URL(string: targetURL!) else {
fatalError("Failed URL")
}
URLSession.shared.dataTask(with: url) { data, _, error in
if error != nil {
print (error)
return
}
self.resultData = data
self.isFinished = true
}.resume
}
}
let operationQueue = //...
let backendOperation = NetworkOperation(url: <Backend-URL>)
let imageOperation = NetworkOperation()
let adapter = BlockOperation() { [unowned backendOperation, unowned imageOperation] in
imageOperation.targetUrl = backendOperation.data // let's imagine this is already converted to string
}
backendOperation => adapter => imageOperation => adapter2 => setImageOperation // we skip the setImage part, as you can see the gist of it
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment