Skip to content

Commit

Permalink
Performing operations on objects within nested contexts where the MR_…
Browse files Browse the repository at this point in the history
…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
ChronicStim committed Jul 9, 2012
1 parent 9164aad commit 80774c0
Showing 1 changed file with 11 additions and 0 deletions.
Expand Up @@ -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];
Expand Down

1 comment on commit 80774c0

@ChronicStim
Copy link
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.