I have a problem that is almost identical to the problem described by this person here, but it hasn\'t get answered:
http://www.cocoabuilder.com/arc
The idea being most of the long hard work and saves can be done on the private MOC
How do you implement that idea? Do you use something like this:
- (void)doSomethingWithDocument:(UIManagedDocument *)document
{
NSManagedObjectContext *parent = document.managedObjectContext.parentContext;
[parent performBlock:^{
/*
Long and expensive tasks..
execute fetch request on parent context
download from remote server
*/
// save document
}];
}
I did above and got deadlock too. Then I tried not to touch the backing queue of parent context. Instead, I use plain and simple GCD to do the downloading stuff, and manipulate core data in child context (on main queue). It works fine. In this way parent context seems useless.. But at least it doesn't cause deadlock..
- (void)doSomethingWithDocument:(UIManagedDocument *)document
{
dispatch_queue_t fetchQ = dispatch_queue_create("Flickr fetcher", NULL);
dispatch_async(fetchQ, ^{
// download from remote server
// perform in the NSMOC's safe thread (main thread)
[document.managedObjectContext performBlock:^{
// execute fetch request on parent context
// save document
}];
});
dispatch_release(fetchQ);
}