I'm using AFNetworking with custom NSOperations and Core data, the Core Data concurrency is well managed and the NSOperation uses a proper Managed object context, something like: (is an abstraction)
// MO created in context A
NSManagedObject * foo = ...;
__Block NSManagedObjectID * fooId = foo.objectID;
NMBlockOperation * customOp = [[NMBlockOperation alloc] initWithBlock:^(NMOperation *operation) {
// MO created in context B
NSManagedObject * safeFoo = [[operation.coreDataManager mocForCurrentThread] objectWithID: fooId];
...
[_networkManager POST:[self apiUrlStringForPath: xxx]
parameters:parameters
success:^(AFHTTPRequestOperation *operation, id responseObject) {
//Do something with the foo object
// MO created in context C
NSManagedObject * safeFoo = [[operation.coreDataManager mocForCurrentThread] objectWithID: fooId];
[operation finish];
} failure:^(AFHTTPRequestOperation *operation, NSError *error) {
[operation fail];
}];
}];
... add customOp to a queue
I'm forced to pass the Managed object from context to context due the fact that the context A, B and C are created in different threads, Is possible force the AFNetworking call to execute the failure/success blocks in the caller's thread? Using this approach I could be able to pass the Managed objects only from context A to the NSOperation's context B.
I know how to set the network manager completion queue but this doesn't help me, I want the same caller's thread.
//configure standard network manager
_networkManager = [[AFHTTPRequestOperationManager alloc] init];
//Configure completation queue
dispatch_queue_t backgroundQueue = dispatch_queue_create("com.xxx.afnetComplQueue", NULL);
_networkManager.completionQueue = backgroundQueue;