Core Data nested managed object contexts and frequent deadlocks / freezes

前端 未结 7 1050
囚心锁ツ
囚心锁ツ 2020-12-04 14:31

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

7条回答
  •  孤街浪徒
    2020-12-04 15:01

    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);
    }
    

提交回复
热议问题