views:

97

answers:

2

Hi all

I have a MOC created in my AppDelegate class. I have a MOC property in my RootView which is set by the AppDelegate with something like:

rootView.managedObjectContext = self.managedObjectContext;

This rootView creates at some time a ModalView and sets there a MOC property also by the same way.

Everything works fine. I can create new managed objects, the views can access them and delete them.

Until the point i make a [self.managedObjectContext save:&error] (in the AppDelegate class when the app moves into background).

After this, if i create a new managed object in the rootView, i can not delete this new object through the modal view with something like: [self.managedObjectContext deleteObject:managedObject]

The app crashes. Sometimes I had an error message in the console like: NSManagedObjectContext cannot delete objects in other contexts.

So it seems that after the save the MOC changes somehow.

What is happening there, what did I miss.

I am using iOS 4.1.

Thx and cheers,

Ben

A: 

Can't tell much without code but I can take a wild guess based on your description.

If you have a custom getter method for the managedObjectContext property in the app delegate that creates the context if it is nil, you might have messed it up causing the accessor to generate a new context each time.

For example, the Xcode template accessors looks like:

- (NSManagedObjectContext *) managedObjectContext {

    if (managedObjectContext != nil) {
        return managedObjectContext;
    }

    NSPersistentStoreCoordinator *coordinator = [self persistentStoreCoordinator];
    if (coordinator != nil) {
        managedObjectContext = [[NSManagedObjectContext alloc] init];
        [managedObjectContext setPersistentStoreCoordinator: coordinator];
    }
    return managedObjectContext;
}

If you did something similar but left out the test for the existing context, you would get a new context each time self.managedObjectContext is called.

TechZen
Right now I can not get my code. I will post more later that day. But I know I have the test for an existing managedObjectContext in my getter method. I can access the managedObjectContext many times without any problems. The error comes after I saved the context. And only for managedObjects I added after saving.
Ben Zwak
A: 

Ok, so this is really strange: Two days ago i quitted working on the project because of the MOC problem. Yesterday evening I started again working on it. I absolutely did not change anything on the code and just wanted to reproduce the error to debug it. And what happend? The error did not appear again. So it seems to that restarting the computer "fixed" the problem. Since then the error did never ever happen again, even after hours of working and testing.

Thanks a lot for help anyway.

Bye,

Ben

Ben Zwak