From 535255a686ad4ff9b540dde94a8961491152f7fe Mon Sep 17 00:00:00 2001 From: Charlie Savage Date: Wed, 6 Mar 2013 01:24:23 -0700 Subject: [PATCH] Fix RKReplaceAssignmentPolicy. The problem was that new values for the relationship were gathered before the old values were deleted. Therefore if one of the new values was the same as the old value, the valueForRelationship collection would contain a deleted managed - not good. Also fixed: * The if statement near line 674 was incorrectly negated * Setting the relationship value around line 676 was redundant since it was done in the mapCoreDataToManyRelationshipValue:withMapping method. --- Code/ObjectMapping/RKMappingOperation.m | 10 ++++++---- 1 file changed, 6 insertions(+), 4 deletions(-) diff --git a/Code/ObjectMapping/RKMappingOperation.m b/Code/ObjectMapping/RKMappingOperation.m index 6739fbf9..b9d042f6 100644 --- a/Code/ObjectMapping/RKMappingOperation.m +++ b/Code/ObjectMapping/RKMappingOperation.m @@ -647,7 +647,12 @@ static NSString * const RKMetadataKeyPathPrefix = @"@metadata."; NSArray *existingObjectsArray = RKTransformedValueWithClass(existingObjects, [NSArray class], nil); [relationshipCollection addObjectsFromArray:existingObjectsArray]; } - + else if (relationshipMapping.assignmentPolicy == RKReplaceAssignmentPolicy) { + if (! [self applyReplaceAssignmentPolicyForRelationshipMapping:relationshipMapping]) { + return NO; + } + } + [value enumerateObjectsUsingBlock:^(id nestedObject, NSUInteger collectionIndex, BOOL *stop) { id mappableObject = [self destinationObjectForMappingRepresentation:nestedObject withMapping:relationshipMapping.mapping inRelationship:relationshipMapping]; if (mappableObject) { @@ -668,9 +673,6 @@ static NSString * const RKMetadataKeyPathPrefix = @"@metadata."; // If the relationship has changed, set it if ([self shouldSetValue:&valueForRelationship atKeyPath:relationshipMapping.destinationKeyPath]) { - if (! [self applyReplaceAssignmentPolicyForRelationshipMapping:relationshipMapping]) { - return NO; - } if (! [self mapCoreDataToManyRelationshipValue:valueForRelationship withMapping:relationshipMapping]) { RKLogTrace(@"Mapped relationship object from keyPath '%@' to '%@'. Value: %@", relationshipMapping.sourceKeyPath, relationshipMapping.destinationKeyPath, valueForRelationship); [self.destinationObject setValue:valueForRelationship forKeyPath:relationshipMapping.destinationKeyPath];