views:

133

answers:

3

Hi there, I have a really strange behavior in a Core Data app. This is the code:

        unaReg.valore = [NSNumber numberWithInt:val];
        NSError *error;
        if (![managedObjectContext save:&error]) {
            [myUtil manageError:[error localizedDescription]];
        }

unaReg is a Core Data Entity and I want to update its valore property. When I run the save command (managedObjectContext save:&error) I get an error since the program flows inside the if branch. The strange behavior is that the error variable is nil and the new value is saved correctly inside the database (so it seems that there is not an error). What's wrong???? Thanks.

+1  A: 

Double check that your managed object context is set:

    unaReg.valore = [NSNumber numberWithInt:val];
    NSError *error;
    NSLog(@"moc = %@",managedObjectContext);
    if (![managedObjectContext save:&error]) {
        [myUtil manageError:[error localizedDescription]];
    }
gerry3
managedObjectContext is set since this code is part of a cycle where I find for the correct unaReg entity
Oscar Peli
+3  A: 

You need to set NSError *error = nil; to be safe otherwise you are going to get an undetermined memory location assigned.

Like gerry3 mentioned, you probably have a nil managedObjectContext. I would recommend changing the code to:

unaReg.valore = [NSNumber numberWithInt:val];
NSError *error = nil;
NSAssert(managedObjectContext != nil, @"Context is nil");
if (![managedObjectContext save:&error]) {
    [myUtil manageError:[error localizedDescription]];
}

This is a perfect use for NSAssert statements, because you can use them to test inline while developing and with one switch, turn them all off for production.

If your managedObjectContext is nil then you will get a false response from -save: and because you did not set error to nil it will be pointing to "something" in memory, further causing confusion.

Marcus S. Zarra
I set the error = nil but I have the same result
Oscar Peli
Since this step is only the last step of an entity-tree trip, I think that the error may be far form here.I have a lot of ViewControllers that send a child entity to a child ViewController as in the next code.myViewController *myVC = [[myViewController alloc] initWithNibName:@"myNib" bundle:nil];myVC.entity = [self entity];myVC.managedObjectContext = self.managedObjectContext;[self.navigationController pushViewController:myVC animated:YES];[myVC release]; Do you Think that the last release causes a memory leak?Thanks
Oscar Peli
No in that sample it would not cause a memory leak. Did you put in the assert as I suggested?
Marcus S. Zarra
Yes I did but now I get an EXC_BAD_ACCESS. I suppose I need to look for bad released objects ... and this will be not a simple task!
Oscar Peli
The assert would not cause a bad access. Sounds like you have more issues than you realized! Have you run your application through the static analyzer? If not you need to and then clean up all of the warnings and analyzer results. ALL of them.
Marcus S. Zarra
This bug is causing me a big headache. Now I get once again a nil error pointer but data are correctly saved inside the Database. Is this a miracle????
Oscar Peli
No, it is an error in your code that you need to track down. Have you run it through the static analyzer? Have you cleaned up all warnings in your code? Can't help you if you don't respond to these questions.
Marcus S. Zarra
I FOUND IT!!!It's quite incredible since I simple forgot to pass the managedObjectContext from parent to child.The incredible think is that data were saved correctly and I can't understand how this was possible!!Thanks.
Oscar Peli
A: 

If the managedObjectContext is not null, then you may need to check other managed objects that you stored in your managedObjectContext as well. Sometimes, the error might be located in the deletion of some other objects, when you just need to save your unaReg object.

What I usually do are:
- Check your data models on the Delete rule, make sure that you have correctly configured all of them - On your save action of managedObjectContext, ensure that other objects are in some way deleted due to the Delete rule.

Some other situation that I have encountered but right now I can not think of.

Hope it helps

sfa