Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Adding an easy to understand identification string to contexts throug…

…h the userInfo property on the context. This is then referenced when using the MR_description method or can be used independently in your own MRLog debug comments. Quite helpful when you're dealing with multiple nested contexts. For example, I might name a context "DiaryEntryEditingContext" or "DiaryEntryDeletionContext" using these methods.

The MR_description method was also updated to show a recursive "family tree" of the contexts by looking for the parentContext, then the parent of the parent, etc. and listing them in the string.
  • Loading branch information...
commit 7d6ef7a45d62ab1a3a4c2e216cb525aa181d297c 1 parent 80774c0
@ChronicStim authored
View
3  MagicalRecord/Categories/NSManagedObjectContext/NSManagedObjectContext+MagicalRecord.h
@@ -25,4 +25,7 @@ extern NSString * const kMagicalRecordDidMergeChangesFromiCloudNotification;
- (NSString *) MR_description;
+- (void) MR_setContextWorkingName:(NSString *)workingName;
+- (NSString *) MR_contextWorkingName;
+
@end
View
28 MagicalRecord/Categories/NSManagedObjectContext/NSManagedObjectContext+MagicalRecord.m
@@ -11,6 +11,7 @@
static NSManagedObjectContext *rootSavingContext = nil;
static NSManagedObjectContext *defaultManagedObjectContext_ = nil;
+#define kNSManagedObjectContextWorkingName @"kNSManagedObjectContextWorkingName"
@interface NSManagedObjectContext (MagicalRecordInternal)
@@ -35,9 +36,16 @@ - (NSString *) MR_description;
NSString *contextName = (self == defaultManagedObjectContext_) ? @"*** DEFAULT ***" : @"";
contextName = (self == rootSavingContext) ? @"*** BACKGROUND SAVE ***" : contextName;
- NSString *onMainThread = [NSThread isMainThread] ? @"*** MAIN THREAD ***" : @"";
+ NSString *onMainThread = [NSThread isMainThread] ? @"*** MAIN THREAD ***" : @"*** SECONDARY THREAD ***";
- return [NSString stringWithFormat:@"%@: %@ Context %@", [self description], contextName, onMainThread];
+ NSString *familyTree = [NSString string];
+ NSManagedObjectContext *parentContext = [self parentContext];
+ while (nil != parentContext) {
+ familyTree = [familyTree stringByAppendingFormat:@" ==> %@;",[parentContext MR_contextWorkingName]];
+ parentContext = [parentContext parentContext];
+ }
+
+ return [NSString stringWithFormat:@"%@: %@ Context %@ \nFamilyTree: %@", [self MR_contextWorkingName], contextName, onMainThread,familyTree];
}
+ (NSManagedObjectContext *) MR_defaultContext
@@ -74,6 +82,7 @@ + (void) MR_setRootSavingContext:(NSManagedObjectContext *)context;
{
rootSavingContext = context;
[rootSavingContext setMergePolicy:NSMergeByPropertyObjectTrumpMergePolicy];
+ [rootSavingContext MR_setContextWorkingName:@"rootSavingsContext"];
}
+ (void) MR_initializeDefaultContextWithCoordinator:(NSPersistentStoreCoordinator *)coordinator;
@@ -85,6 +94,7 @@ + (void) MR_initializeDefaultContextWithCoordinator:(NSPersistentStoreCoordinato
[self MR_setRootSavingContext:rootContext];
NSManagedObjectContext *defaultContext = [self MR_newMainQueueContext];
+ [defaultContext MR_setContextWorkingName:@"defaultContext"];
[defaultContext setParentContext:rootSavingContext];
[self MR_setDefaultContext:defaultContext];
@@ -149,5 +159,19 @@ + (NSManagedObjectContext *) MR_contextWithStoreCoordinator:(NSPersistentStoreCo
return context;
}
+- (void) MR_setContextWorkingName:(NSString *)workingName;
+{
+ [[self userInfo] setObject:workingName forKey:kNSManagedObjectContextWorkingName];
+}
+
+- (NSString *) MR_contextWorkingName;
+{
+ NSString *workingName = [[self userInfo] objectForKey:kNSManagedObjectContextWorkingName];
+ if (nil == workingName) {
+ workingName = @"UndefinedWorkingContext";
+ }
+ return workingName;
+}
+
@end
Please sign in to comment.
Something went wrong with that request. Please try again.