Skip to content

Commit

Permalink
Merge pull request 3lvis#147 from tpmullan/master
Browse files Browse the repository at this point in the history
Added feature to sync relationships with only ID
3lvis committed Nov 25, 2015
2 parents ab9fbc0 + 6ff5f09 commit 1b0c492
Showing 1 changed file with 29 additions and 0 deletions.
29 changes: 29 additions & 0 deletions Source/NSManagedObject+Sync.m
Original file line number Diff line number Diff line change
@@ -54,6 +54,7 @@ - (void)sync_processRelationshipsUsingDictionary:(NSDictionary *)objectDictionar
NSArray *relationships = [self.entity sync_relationships];

for (NSRelationshipDescription *relationship in relationships) {
NSString *keyName = [[relationship.destinationEntity.name lowercaseString] stringByAppendingString:@"_id"];
if (relationship.isToMany) {
[self sync_processToManyRelationship:relationship
usingDictionary:objectDictionary
@@ -63,6 +64,13 @@ - (void)sync_processRelationshipsUsingDictionary:(NSDictionary *)objectDictionar
[relationship.destinationEntity.name isEqualToString:parent.entity.name]) {
[self setValue:parent
forKey:relationship.name];
} else if ([objectDictionary objectForKey:keyName]) {
NSError *error = nil;
[self sync_processIdRelationship:relationship
remoteID:[objectDictionary objectForKey:keyName]
andParent:parent
dataStack:dataStack
error:&error];
} else {
NSError *error = nil;
[self sync_processToOneRelationship:relationship
@@ -156,4 +164,25 @@ - (void)sync_processToOneRelationship:(NSRelationshipDescription *)relationship
}
}

- (void)sync_processIdRelationship:(NSRelationshipDescription *)relationship
remoteID:(NSNumber *)remoteID
andParent:(NSManagedObject *)parent
dataStack:(DATAStack *)dataStack
error:(NSError **)error {

NSString *entityName = relationship.destinationEntity.name;

NSError *errors = nil;
NSManagedObject *object = [NSManagedObject sync_safeObjectInContext:self.managedObjectContext
entityName:entityName
remoteID:remoteID
parent:self
parentRelationshipName:relationship.name
error:&errors];
if (object) {
[self setValue:object
forKey:relationship.name];
}
}

@end

0 comments on commit 1b0c492

Please sign in to comment.