Created
December 20, 2010 11:30
-
-
Save fpillet/748276 to your computer and use it in GitHub Desktop.
Core data helpers to log errors and changes to a context (using NSLogger)
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
- (void)logDetailedError:(NSError *)error from:(id)caller selector:(SEL)selector | |
{ | |
#if DEBUG | |
LogMessage(@"coredata", 0, @"*** CORE DATA ERROR: a data store operation failed"); | |
LogMessage(@"coredata", 0, @"*** Caller was: %@ %p %@", [caller class], caller, NSStringFromSelector(selector)); | |
LogMessage(@"coredata", 0, @"*** Error: %@", [error localizedDescription]); | |
NSArray* detailedErrors = [[error userInfo] objectForKey:NSDetailedErrorsKey]; | |
if ([detailedErrors count] > 0) | |
{ | |
for(NSError* detailedError in detailedErrors) | |
LogMessage(@"coredata", 0, @"> DetailedError: %@", [detailedError userInfo]); | |
} | |
else | |
{ | |
LogMessage(@"coredata", 0, @" %@", [error userInfo]); | |
} | |
#endif | |
} | |
- (void)logContextChanges | |
{ | |
#if DEBUG | |
// Log the current changes for the context | |
if (![self hasChanges]) | |
return; | |
LogMessage(@"coredata", 1, @"***************************************************"); | |
LogMessage(@"coredata", 1, @"* CHANGES TO %@ %p", [self class], self); | |
LogMessage(@"coredata", 1, @"***************************************************"); | |
NSSet *updated = [self updatedObjects]; | |
NSSet *inserted = [self insertedObjects]; | |
NSSet *deleted = [self deletedObjects]; | |
if ([updated count]) | |
{ | |
LogMessage(@"coredata", 0, @"* UPDATED OBJECTS:"); | |
for (NSManagedObject *anObject in [self updatedObjects]) | |
{ | |
LogMessage(@"coredata", 0, @"* %@ %p has the following changes:", [anObject class], anObject); | |
NSDictionary *changedValues = [anObject changedValues]; | |
NSArray *keys = [changedValues allKeys]; | |
NSDictionary *oldValues = [anObject committedValuesForKeys:keys]; | |
for (NSString *key in keys) | |
LogMessage(@"coredata", 0, @" Attribute '%@' was {%@} is now {%@}", key, [oldValues objectForKey:key], [changedValues objectForKey:key]); | |
LogMessage(@"coredata", 0, @"*"); | |
} | |
} | |
if ([inserted count]) | |
{ | |
if ([updated count]) | |
LogMessage(@"coredata", 0, @"***************************************************"); | |
LogMessage(@"coredata", 0, @"* INSERTED OBJECTS:"); | |
for (NSManagedObject *anObject in [self insertedObjects]) | |
{ | |
LogMessage(@"coredata", 0, @"* %@", anObject); | |
LogMessage(@"coredata", 0, @"*"); | |
} | |
} | |
if ([deleted count]) | |
{ | |
if ([updated count] || [inserted count]) | |
LogMessage(@"coredata", 0, @"***************************************************"); | |
LogMessage(@"coredata", 0, @"* DELETED OBJECTS:"); | |
for (NSManagedObject *anObject in [self deletedObjects]) | |
{ | |
LogMessage(@"coredata", 0, @"* %@", anObject); | |
LogMessage(@"coredata", 0, @"*"); | |
} | |
} | |
LogMessage(@"coredata", 0, @"***************************************************"); | |
#endif | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment