Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Separate printing context chain method into its own method

change contextWorkingName to property workingName
  • Loading branch information...
commit 0fb7d36f4dbda94889528823cd2130a12e4664ca 1 parent 64a81c6
@casademora casademora authored
View
4 MagicalRecord/Categories/NSManagedObjectContext/NSManagedObjectContext+MagicalRecord.h
@@ -23,8 +23,8 @@ extern NSString * const kMagicalRecordDidMergeChangesFromiCloudNotification;
+ (NSManagedObjectContext *) MR_defaultContext;
- (NSString *) MR_description;
+- (NSString *) MR_parentChain;
-- (void) MR_setContextWorkingName:(NSString *)workingName;
-- (NSString *) MR_contextWorkingName;
+@property (nonatomic, copy, setter = MR_setWorkingName:) NSString *MR_workingName;
@end
View
67 MagicalRecord/Categories/NSManagedObjectContext/NSManagedObjectContext+MagicalRecord.m
@@ -12,7 +12,7 @@
static NSManagedObjectContext *defaultManagedObjectContext_ = nil;
static id iCloudSetupNotificationObserver = nil;
-#define kNSManagedObjectContextWorkingName @"kNSManagedObjectContextWorkingName"
+static NSString * const kMagicalRecordNSManagedObjectContextWorkingName = @"kNSManagedObjectContextWorkingName";
@interface NSManagedObjectContext (MagicalRecordInternal)
@@ -34,19 +34,23 @@ + (void) MR_cleanUp;
- (NSString *) MR_description;
{
- NSString *contextName = (self == defaultManagedObjectContext_) ? @"*** DEFAULT ***" : @"";
- contextName = (self == rootSavingContext) ? @"*** BACKGROUND SAVE ***" : contextName;
-
- NSString *onMainThread = [NSThread isMainThread] ? @"*** MAIN THREAD ***" : @"*** SECONDARY THREAD ***";
-
- NSString *familyTree = [NSString string];
- NSManagedObjectContext *parentContext = [self parentContext];
- while (nil != parentContext) {
- familyTree = [familyTree stringByAppendingFormat:@" ==> %@;",[parentContext MR_contextWorkingName]];
- parentContext = [parentContext parentContext];
+ NSString *contextLabel = [NSString stringWithFormat:@"*** %@ ***", [self MR_workingName]];
+ NSString *onMainThread = [NSThread isMainThread] ? @"*** MAIN THREAD ***" : @"*** BACKGROUND THREAD ***";
+
+ return [NSString stringWithFormat:@"<%@ (%p): %@> on %@", NSStringFromClass([self class]), self, contextLabel, onMainThread];
+}
+
+- (NSString *) MR_parentChain;
+{
+ NSMutableString *familyTree = [@"" mutableCopy];
+ NSManagedObjectContext *currentContext = self;
+ do
+ {
+ [familyTree appendFormat:@"- %@ (%p) %@\n", [currentContext MR_workingName], currentContext, (currentContext == self ? @"(*)" : @"")];
}
+ while ((currentContext = [currentContext parentContext]));
- return [NSString stringWithFormat:@"%@: %@ Context %@ \nFamilyTree: %@", [self MR_contextWorkingName], contextName, onMainThread,familyTree];
+ return [NSString stringWithString:familyTree];
}
+ (NSManagedObjectContext *) MR_defaultContext
@@ -66,7 +70,8 @@ + (void) MR_setDefaultContext:(NSManagedObjectContext *)moc
}
NSPersistentStoreCoordinator *coordinator = [NSPersistentStoreCoordinator MR_defaultStoreCoordinator];
- if (iCloudSetupNotificationObserver) {
+ if (iCloudSetupNotificationObserver)
+ {
[[NSNotificationCenter defaultCenter] removeObserver:iCloudSetupNotificationObserver];
iCloudSetupNotificationObserver = nil;
}
@@ -77,8 +82,10 @@ + (void) MR_setDefaultContext:(NSManagedObjectContext *)moc
}
defaultManagedObjectContext_ = moc;
- [moc MR_obtainPermanentIDsBeforeSaving];
- if ([MagicalRecord isICloudEnabled])
+ [defaultManagedObjectContext_ MR_setWorkingName:@"DEFAULT"];
+
+// [moc MR_obtainPermanentIDsBeforeSaving];
+ if ([MagicalRecord isICloudEnabled])
{
[defaultManagedObjectContext_ MR_observeiCloudChangesInCoordinator:coordinator];
}
@@ -92,6 +99,7 @@ + (void) MR_setDefaultContext:(NSManagedObjectContext *)moc
[[NSManagedObjectContext MR_defaultContext] MR_observeiCloudChangesInCoordinator:coordinator];
}];
}
+ MRLog(@"Set Default Context: %@", defaultManagedObjectContext_);
}
+ (NSManagedObjectContext *) MR_rootSavingContext;
@@ -107,9 +115,10 @@ + (void) MR_setRootSavingContext:(NSManagedObjectContext *)context;
}
rootSavingContext = context;
- [context MR_obtainPermanentIDsBeforeSaving];
+// [context MR_obtainPermanentIDsBeforeSaving];
[rootSavingContext setMergePolicy:NSMergeByPropertyObjectTrumpMergePolicy];
- [rootSavingContext MR_setContextWorkingName:@"rootSavingsContext"];
+ [rootSavingContext MR_setWorkingName:@"BACKGROUND SAVING (ROOT)"];
+ MRLog(@"Set Root Saving Context: %@", rootSavingContext);
}
+ (void) MR_initializeDefaultContextWithCoordinator:(NSPersistentStoreCoordinator *)coordinator;
@@ -117,14 +126,12 @@ + (void) MR_initializeDefaultContextWithCoordinator:(NSPersistentStoreCoordinato
if (defaultManagedObjectContext_ == nil)
{
NSManagedObjectContext *rootContext = [self MR_contextWithStoreCoordinator:coordinator];
-
[self MR_setRootSavingContext:rootContext];
NSManagedObjectContext *defaultContext = [self MR_newMainQueueContext];
- [defaultContext MR_setContextWorkingName:@"defaultContext"];
- [defaultContext setParentContext:rootSavingContext];
-
[self MR_setDefaultContext:defaultContext];
+
+ [defaultContext setParentContext:rootSavingContext];
}
}
@@ -154,13 +161,14 @@ + (NSManagedObjectContext *) MR_contextWithParent:(NSManagedObjectContext *)pare
{
NSManagedObjectContext *context = [self MR_contextWithoutParent];
[context setParentContext:parentContext];
- [context MR_obtainPermanentIDsBeforeSaving];
+// [context MR_obtainPermanentIDsBeforeSaving];
return context;
}
+ (NSManagedObjectContext *) MR_newMainQueueContext;
{
NSManagedObjectContext *context = [[self alloc] initWithConcurrencyType:NSMainQueueConcurrencyType];
+ MRLog(@"Created Main Queue Context: %@", context);
return context;
}
@@ -194,7 +202,7 @@ - (void) MR_contextWillSave:(NSNotification *)notification
if ([insertedObjects count])
{
- MRLog(@"Context %@ is about to save. Obtaining permanent IDs for new %lu inserted objects", [context MR_description], (unsigned long)[insertedObjects count]);
+ MRLog(@"Context %@ is about to save. Obtaining permanent IDs for new %lu inserted objects", [context MR_workingName], (unsigned long)[insertedObjects count]);
NSError *error = nil;
BOOL success = [context obtainPermanentIDsForObjects:[insertedObjects allObjects] error:&error];
if (!success)
@@ -204,16 +212,17 @@ - (void) MR_contextWillSave:(NSNotification *)notification
}
}
-- (void) MR_setContextWorkingName:(NSString *)workingName;
+- (void) MR_setWorkingName:(NSString *)workingName;
{
- [[self userInfo] setObject:workingName forKey:kNSManagedObjectContextWorkingName];
+ [[self userInfo] setObject:workingName forKey:kMagicalRecordNSManagedObjectContextWorkingName];
}
-- (NSString *) MR_contextWorkingName;
+- (NSString *) MR_workingName;
{
- NSString *workingName = [[self userInfo] objectForKey:kNSManagedObjectContextWorkingName];
- if (nil == workingName) {
- workingName = @"UndefinedWorkingContext";
+ NSString *workingName = [[self userInfo] objectForKey:kMagicalRecordNSManagedObjectContextWorkingName];
+ if (nil == workingName)
+ {
+ workingName = @"UNNAMED";
}
return workingName;
}
View
8 MagicalRecord/Categories/NSManagedObjectContext/NSManagedObjectContext+MagicalSaves.m
@@ -96,11 +96,9 @@ - (void) MR_saveErrorHandler:(void (^)(NSError *))errorCallback;
[self performBlockAndWait:^{
[self MR_saveWithErrorCallback:errorCallback];
- if (self.parentContext) {
- [[self parentContext] performBlockAndWait:^{
- [[self parentContext] MR_saveErrorHandler:errorCallback];
- }];
- }
+ [[self parentContext] performBlockAndWait:^{
+ [[self parentContext] MR_saveErrorHandler:errorCallback];
+ }];
}];
}
View
13 MagicalRecord/Core/MagicalRecord.m
@@ -42,12 +42,13 @@ + (void) cleanUpStack;
+ (NSString *) currentStack
{
NSMutableString *status = [NSMutableString stringWithString:@"Current Default Core Data Stack: ---- \n"];
-
- [status appendFormat:@"Context: %@\n", [NSManagedObjectContext MR_defaultContext]];
- [status appendFormat:@"Model: %@\n", [[NSManagedObjectModel MR_defaultManagedObjectModel] entityVersionHashesByName]];
- [status appendFormat:@"Coordinator: %@\n", [NSPersistentStoreCoordinator MR_defaultStoreCoordinator]];
- [status appendFormat:@"Store: %@\n", [NSPersistentStore MR_defaultPersistentStore]];
-
+
+ [status appendFormat:@"Model: %@\n", [[NSManagedObjectModel MR_defaultManagedObjectModel] entityVersionHashesByName]];
+ [status appendFormat:@"Coordinator: %@\n", [NSPersistentStoreCoordinator MR_defaultStoreCoordinator]];
+ [status appendFormat:@"Store: %@\n", [NSPersistentStore MR_defaultPersistentStore]];
+ [status appendFormat:@"Default Context: %@\n", [[NSManagedObjectContext MR_defaultContext] MR_description]];
+ [status appendFormat:@"Context Chain: \n%@\n", [[NSManagedObjectContext MR_defaultContext] MR_parentChain]];
+
return status;
}

1 comment on commit 0fb7d36

@gorbster

Why are all instances of [moc MR_obtainPermanentIDsBeforeSaving]; commented out in this commit?

This exposes a bug in iOS 5/5.1 where fetch results are broken for child contexts after inserts are made and saved to the root context. The only workaround for iOS 5/5.1 is to obtain permanent IDs on save (though it appears this has been somewhat resolved in iOS 6).

The following article explains more:

http://wbyoung.tumblr.com/post/27851725562/core-data-growing-pains ("Fetch results are wrong in a child context")

Please sign in to comment.
Something went wrong with that request. Please try again.