Browse files

Performing operations on objects within nested contexts where the MR_…

…inContext: method was used was throwing errors when objects with tempID's were being passed to the method. The solution was to check if the ID is temporary and then if it is, request a permanentID before continuing.
  • Loading branch information...
1 parent 9164aad commit 80774c01258d85a8ea48c25480c975f8ff56ed14 @ChronicStim committed Jul 9, 2012
Showing with 11 additions and 0 deletions.
  1. +11 −0 MagicalRecord/Categories/NSManagedObject/NSManagedObject+MagicalRecord.m
11 MagicalRecord/Categories/NSManagedObject/NSManagedObject+MagicalRecord.m
@@ -221,6 +221,17 @@ + (BOOL) MR_truncateAll
- (id) MR_inContext:(NSManagedObjectContext *)otherContext
+ NSManagedObjectID *objectID = [self objectID];
+ if ([objectID isTemporaryID]) {
+ MRLog(@"Object (%@) has temporaryID. Attempting to generate permanentID.",[[self class] description]);
+ NSError *error = nil;
+ if ([[self managedObjectContext] obtainPermanentIDsForObjects:[NSArray arrayWithObject:self] error:&error]) {
+ objectID = [self objectID];
+ MRLog(@"Object (%@) was granted a permanentID.",[[self class] description]);
+ } else {
+ [MagicalRecord handleErrors:error];
+ }
+ }
NSError *error = nil;
NSManagedObject *inContext = [otherContext existingObjectWithID:[self objectID] error:&error];
[MagicalRecord handleErrors:error];

1 comment on commit 80774c0


Oopps! The existing line wasn't updated to use the new variable "objectID". It should also be updated to look like:

NSManagedObject *inContext = [otherContext existingObjectWithID:objectID error:&error];

The code still works even without the change since the [self objectID] will now pull a perm id, but for accuracy it should be changed. Thanks.

Please sign in to comment.