One of the crazy things about it is if you get an error and try to figure things out by doing this:
NSError *error;
if (![managedObjectContext save:&error])
{
NSLog(@"Error saving User: %@", [error localizedDescription]);
}
You get a good error - something like "Operation could not be completed: cocoa error code 1560".
Not very helpful. I just stumbled across this little nugget of information from back in 2009! All of these years of suffering I have been through. Basically the gist of the article is you create a macro like this:
#define FT_SAVE_MOC(_ft_moc) \
do { \
NSError* _ft_save_error; \
if(![_ft_moc save:&_ft_save_error]) { \
NSLog(@"Failed to save to data store: %@", [_ft_save_error localizedDescription]); \
NSArray* _ft_detailedErrors = [[_ft_save_error userInfo] objectForKey:NSDetailedErrorsKey]; \
if(_ft_detailedErrors != nil && [_ft_detailedErrors count] > 0) { \
for(NSError* _ft_detailedError in _ft_detailedErrors) { \
NSLog(@"DetailedError: %@", [_ft_detailedError userInfo]); \
} \
} \
else { \
NSLog(@"%@", [_ft_save_error userInfo]); \
} \
} \
} while(0);
and when you save like this:
FT_SAVE_MOC(managedObjectContext);
If you get an error you actually get useful information!
Core Data just got better!
-Aaron
No comments:
Post a Comment