The new Cloud Kit framework uses NSOperation extensively for it's CRUD. The results of those operations are returned in blocks. For example:
let fetchOperation = CKFetchRecordsOperation(recordIDs: [recordID1, recordId2])
fetchOperation.fetchRecordsCompletionBlock = {(dict: NSDictionary!, error: NSError!) -> Void in
// dict contains RecordId -> Record
// do something with the records here (if no error)
}
I want to chain a few of those operations (dependencies), and pass the result of an operation to the next operation in the chain. Simplified example to illustrate this (pseudo code!):
let fetchOperation1 = CKFetchRecordsOperation(recordIDs: [recordID1, recordId2])
fetchOperation1.fetchRecordsCompletionBlock = {(dict: NSDictionary!, error: NSError!) -> Void in
if error {
// handle error
} else {
// dict contains RecordId -> Record
// let's pretend our records contain references to other records
// that we want to fetch as well
fetchOperation.operationResult =
dict.allValues().map(
{ $0.getObject("referencedRecordId"}
)
}
}
let fetchOperation2 = CKFetchRecordsOperation(recordIDs: fetchOperation1.operationResult)
fetchOperation2.fetchRecordsCompletionBlock = {(dict: NSDictionary!, error: NSError!) -> Void in
if error {
// handle error
} else {
// dosomething
}
}
fetchOperation2.addDependency(fetchOperation2)
But above pseudo code can never work, as the fetchOperation1.operationResult is not yet assigned when you init fetchOperation2. You could nest the init of fetchOperation2 in fetchOperation1's completionBlock, but than you ditch the dependency functionality of NSOperation, which I'm trying to use here.
So, I'm looking for a clean, readable, standard (no reactive cocoa and such) solution to work have NSOperation dependencies pass data along in their chain.