mirror of
https://github.com/zhigang1992/RestKit.git
synced 2026-01-12 22:51:50 +08:00
Add support for skipping relationship connections if all connection attributes evaluate to nil. Add support for establishing connections when some connection attributes evaluate to nil. fixes #1099 closes #1102
This commit is contained in:
@@ -45,9 +45,9 @@ static NSDictionary *RKConnectionAttributeValuesWithObject(RKConnectionDescripti
|
||||
for (NSString *sourceAttribute in connection.attributes) {
|
||||
NSString *destinationAttribute = [connection.attributes objectForKey:sourceAttribute];
|
||||
id sourceValue = [managedObject valueForKey:sourceAttribute];
|
||||
[destinationEntityAttributeValues setValue:sourceValue forKey:destinationAttribute];
|
||||
[destinationEntityAttributeValues setValue:sourceValue ?: [NSNull null] forKey:destinationAttribute];
|
||||
}
|
||||
return destinationEntityAttributeValues;
|
||||
return [[destinationEntityAttributeValues allValues] isEqualToArray:@[ [NSNull null] ]] ? nil : destinationEntityAttributeValues;
|
||||
}
|
||||
|
||||
@interface RKRelationshipConnectionOperation ()
|
||||
@@ -143,6 +143,7 @@ static NSDictionary *RKConnectionAttributeValuesWithObject(RKConnectionDescripti
|
||||
|
||||
if ([self.connection isForeignKeyConnection]) {
|
||||
NSDictionary *attributeValues = RKConnectionAttributeValuesWithObject(self.connection, self.managedObject);
|
||||
if ([attributeValues count] == 0) return nil;
|
||||
NSSet *managedObjects = [self.managedObjectCache managedObjectsWithEntity:[self.connection.relationship destinationEntity]
|
||||
attributeValues:attributeValues
|
||||
inManagedObjectContext:self.managedObjectContext];
|
||||
|
||||
@@ -301,4 +301,43 @@
|
||||
assertThat(human.cats, hasItems(asia, nil));
|
||||
}
|
||||
|
||||
- (void)testConnectionOfOptionalRelationshipIsSkippedWhenAllConnectionAttributesEvaluateToNil
|
||||
{
|
||||
RKHuman *human = [RKTestFactory insertManagedObjectForEntityForName:@"Human" inManagedObjectContext:nil withProperties:nil];
|
||||
RKCat __unused *asia = [RKTestFactory insertManagedObjectForEntityForName:@"Cat" inManagedObjectContext:nil withProperties:@{@"birthYear": @2011}];
|
||||
RKCat __unused *lola = [RKTestFactory insertManagedObjectForEntityForName:@"Cat" inManagedObjectContext:nil withProperties:@{@"birthYear": @2012}];
|
||||
|
||||
RKEntityMapping *mapping = [RKEntityMapping mappingForEntityForName:@"Human" inManagedObjectStore:[RKTestFactory managedObjectStore]];
|
||||
[mapping addConnectionForRelationship:@"cats" connectedBy:@"sex"];
|
||||
RKFetchRequestManagedObjectCache *managedObjectCache = [RKFetchRequestManagedObjectCache new];
|
||||
RKConnectionDescription *connection = [mapping connectionForRelationship:@"cats"];
|
||||
|
||||
RKRelationshipConnectionOperation *operation = [[RKRelationshipConnectionOperation alloc] initWithManagedObject:human connection:connection managedObjectCache:managedObjectCache];
|
||||
[operation start];
|
||||
assertThat(human.cats, hasCountOf(0));
|
||||
}
|
||||
|
||||
- (void)testConnectionOfOptionalRelationshipIsEvaluatedWhenAtLeastOneAttributeEvaluatesToNonNil
|
||||
{
|
||||
RKHuman *human = [RKTestFactory insertManagedObjectForEntityForName:@"Human" inManagedObjectContext:nil withProperties:nil];
|
||||
human.sex = @"female";
|
||||
|
||||
RKCat *asia = [RKTestFactory insertManagedObjectForEntityForName:@"Cat" inManagedObjectContext:nil withProperties:@{@"birthYear": @2011}];
|
||||
asia.sex = @"female";
|
||||
asia.name = @"Asia";
|
||||
RKCat *lola = [RKTestFactory insertManagedObjectForEntityForName:@"Cat" inManagedObjectContext:nil withProperties:@{@"birthYear": @2012}];
|
||||
lola.sex = @"female";
|
||||
lola.name = nil;
|
||||
|
||||
RKEntityMapping *mapping = [RKEntityMapping mappingForEntityForName:@"Human" inManagedObjectStore:[RKTestFactory managedObjectStore]];
|
||||
[mapping addConnectionForRelationship:@"cats" connectedBy:@[ @"sex", @"name" ]];
|
||||
RKFetchRequestManagedObjectCache *managedObjectCache = [RKFetchRequestManagedObjectCache new];
|
||||
RKConnectionDescription *connection = [mapping connectionForRelationship:@"cats"];
|
||||
|
||||
RKRelationshipConnectionOperation *operation = [[RKRelationshipConnectionOperation alloc] initWithManagedObject:human connection:connection managedObjectCache:managedObjectCache];
|
||||
[operation start];
|
||||
assertThat(human.cats, hasCountOf(1));
|
||||
assertThat(human.cats, hasItems(lola, nil));
|
||||
}
|
||||
|
||||
@end
|
||||
|
||||
Reference in New Issue
Block a user