views:

24

answers:

1

On iPhone, I´ve tried to delete managed objects in background:

- (void) cleanUp {
    dispatch_queue_t queue_cleanUp;
    queue_cleanUp = dispatch_queue_create("com.aroundcal.cleanup", NULL);
     dispatch_async(queue_cleanUp, ^{
        while (!self.stopThread) {      
            [self deleteMyObjects]; 
            [NSThread sleepForTimeInterval:30];  
        }
    }); 
    dispatch_release(queue_cleanUp);
}

In deleteObjects I use a separate managedObjectContext. If I delete some of these objects from the user interface, the app crashes if the cleanUp thread runs in the background. The error is that faults cannot be full filled.

Can someone help?

+1  A: 

You are crashing because the foreground context is unaware that the background context has deleted objects from the persistent store. When the foreground context attempts to fault in the deleted objects, it crashes.

You will need to merge the changes made in the background thread's context with the foreground thread's context. You need to register the foreground context as an observer of the background context so that it can update itself before trying to fault in any new objects.

The Core Data Books: RootController.m has a good example of how to do that. The file is fairly long. Do a find for mergeChangesFromContextDidSaveNotification: and read the detailed comments.

TechZen
Thanks for this answer!
Diethard Seiferth
A checkmark would be appreciated and it helps you accepted answer rate as well.
TechZen