Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

managedObjectContext save fails with error being null

What would cause an error to come back null, this is what I have

    + (BOOL)saveContext:(NSManagedObjectContext *)context
    {
      NSError *error = nil;
      if (![context save:&error]) 
      {
        DLog(@"ERROR %@, %@", error, [error userInfo]);
        UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Sorry"

                                                        message:@"Error Saving the Data" 
                                                       delegate:nil 
                                              cancelButtonTitle:@"OK"
                                              otherButtonTitles:nil];

        [alert show];
        [alert release];

        return NO;
      }
    return YES;
    }

The above method is a class method, I am not sure why the error does not have any information.

This method is called like this

[HSCoreDataUtility saveContext:self.managedObjectContext];

when a modalViewController is closing and returning to the NavigationController, so I need the context to be saved, but it throughs an error, now I think I have an idea as to the cause of it not being saved, but shouldn't the error give me a clue? but the log just says "ERROR (null), (null)"

any thoughts

like image 270
creativeKoder Avatar asked Nov 09 '10 19:11

creativeKoder


2 Answers

I lived your pain and end up surviving. ;)

After long debugging time I realized in custom object validation method, in some cases I was returning NO and not initializing an error.

If you have this problem check validation object methods. That probably was your problem as well.

like image 169
C. Miranda Avatar answered Nov 03 '22 16:11

C. Miranda


Only way I can see that situation happening is if you are passing in a nil context. I would put a logic bomb at the top of that class method to guard against that.

Well, actually, I wouldn't create a class method for this small amount of code; but the point still stands. Check for a nil context.

like image 20
Marcus S. Zarra Avatar answered Nov 03 '22 14:11

Marcus S. Zarra