Hi
The sample code below I lifted from another another post, let's go with this example to describe what I'm trying to do.
https://forums.developer.apple.com/message/86598#
Let's say fetchOp fails due to an authentication error. I want to kick off another operation to re-authenticate, then retry the fetchOp. What's would be the best approach for achieving this?
class FetchOperation: NSOperation {
var url: NSURL?
var data: NSData?
var error: NSError?
convenience init(url: NSURL) {
self.init()
self.url = url
}
// I've omitted the intricacies of implementing an async
// network operation because that’s too complex a topic to
// cover here. The only thing to watch out for is that, for
// adapter operations to work in a fully composible fashion,
// the start of the async code must check self.error and fail
// immediately if it’s set.
}
class ParseOperation: NSOperation {
var data: NSData?
var error: NSError?
convenience init(data: NSData) {
self.init()
self.data = data
}
override func main() {
if self.error == nil {
if let data = self.data {
self.parseData(data)
} else {
// We have no data to parse; this shouldn't happen.
fatalError()
}
}
}
func parseData(data: NSData) {
// ... parse the data ...
}
}
let fetchOp = FetchOperation(url: url)
let parseOp = ParseOperation()
let adapterOp = NSBlockOperation(block: {
parseOp.data = fetchOp.data
parseOp.error = fetchOp.error
})
adapterOp.addDependency(fetchOp)
parseOp.addDependency(adapterOp)
networkQueue.addOperation(fetchOp)
computeQueue.addOperation(adapterOp)
computeQueue.addOperation(parseOp) Many thanks