Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

#1802 don't reuse deleted objects when mapping a relationship with identification attributes and managed object cache #1881

Closed
2 changes: 1 addition & 1 deletion Code/CoreData/RKManagedObjectMappingOperationDataSource.m
Expand Up @@ -271,7 +271,7 @@ - (id)mappingOperation:(RKMappingOperation *)mappingOperation targetObjectForRep
}
}

if (managedObject == nil) {
if (managedObject == nil || [managedObject isDeleted]) {
managedObject = [[NSManagedObject alloc] initWithEntity:entity insertIntoManagedObjectContext:self.managedObjectContext];
[managedObject setValuesForKeysWithDictionary:entityIdentifierAttributes];
if (entityMapping.persistentStore) [self.managedObjectContext assignObject:managedObject toPersistentStore:entityMapping.persistentStore];
Expand Down
6 changes: 4 additions & 2 deletions Tests/Logic/ObjectMapping/RKObjectMappingNextGenTest.m
Expand Up @@ -2050,12 +2050,14 @@ - (void)testReplacmentPolicyForToManyCoreDataRelationshipDeletesExistingValues
- (void)testReplacmentPolicyForToManyCoreDataRelationshipDoesNotDeleteNewValuesOnSecondMapping
{
RKManagedObjectStore *managedObjectStore = [RKTestFactory managedObjectStore];
managedObjectStore.managedObjectCache = [[RKInMemoryManagedObjectCache alloc] initWithManagedObjectContext:managedObjectStore.persistentStoreManagedObjectContext];
RKHuman *human = [NSEntityDescription insertNewObjectForEntityForName:@"Human" inManagedObjectContext:managedObjectStore.mainQueueManagedObjectContext];

RKEntityMapping *entityMapping = [RKEntityMapping mappingForEntityForName:@"Human" inManagedObjectStore:managedObjectStore];
[entityMapping addAttributeMappingsFromArray:@[ @"name" ]];
RKEntityMapping *catMapping = [RKEntityMapping mappingForEntityForName:@"Cat" inManagedObjectStore:managedObjectStore];
[catMapping addAttributeMappingsFromArray:@[ @"name" ]];
catMapping.identificationAttributes = @[ @"name" ];
RKRelationshipMapping *relationshipMapping = [RKRelationshipMapping relationshipMappingFromKeyPath:@"cats" toKeyPath:@"cats" withMapping:catMapping];
relationshipMapping.assignmentPolicy = RKReplaceAssignmentPolicy;
[entityMapping addPropertyMapping:relationshipMapping];
Expand All @@ -2064,7 +2066,7 @@ - (void)testReplacmentPolicyForToManyCoreDataRelationshipDoesNotDeleteNewValuesO
NSDictionary *dictionary = @{ @"name": @"Blake", @"cats": @[ @{ @"name": @"Roy" } ] };

RKMappingOperation *firstOperation = [[RKMappingOperation alloc] initWithSourceObject:dictionary destinationObject:human mapping:entityMapping];
RKManagedObjectMappingOperationDataSource *firstDataSource = [[RKManagedObjectMappingOperationDataSource alloc] initWithManagedObjectContext:managedObjectStore.mainQueueManagedObjectContext cache:nil];
RKManagedObjectMappingOperationDataSource *firstDataSource = [[RKManagedObjectMappingOperationDataSource alloc] initWithManagedObjectContext:managedObjectStore.mainQueueManagedObjectContext cache:managedObjectStore.managedObjectCache];
firstOperation.dataSource = firstDataSource;

[firstOperation performMapping:&error];
Expand All @@ -2075,7 +2077,7 @@ - (void)testReplacmentPolicyForToManyCoreDataRelationshipDoesNotDeleteNewValuesO
assertThat(firstCatNames, hasItems(@"Roy", nil));

RKMappingOperation *secondOperation = [[RKMappingOperation alloc] initWithSourceObject:dictionary destinationObject:human mapping:entityMapping];
RKManagedObjectMappingOperationDataSource *secondDataSource = [[RKManagedObjectMappingOperationDataSource alloc] initWithManagedObjectContext:managedObjectStore.mainQueueManagedObjectContext cache:nil];
RKManagedObjectMappingOperationDataSource *secondDataSource = [[RKManagedObjectMappingOperationDataSource alloc] initWithManagedObjectContext:managedObjectStore.mainQueueManagedObjectContext cache:managedObjectStore.managedObjectCache];
secondOperation.dataSource = secondDataSource;

[secondOperation performMapping:&error];
Expand Down