Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
40 changes: 25 additions & 15 deletions Source/MMRecord/MMRecordResponse.m
Original file line number Diff line number Diff line change
Expand Up @@ -161,28 +161,34 @@ - (void)uniquelyAddNewProtoRecord:(MMRecordProtoRecord *)protoRecord
[responseGroup addProtoRecord:protoRecord];
}

#pragma mark - Determine Entity subclass to use

- (NSEntityDescription *)subEntityForRecordResponseObject:(id)object
withInitialEntity:(NSEntityDescription *)initialEntity {
NSArray *subEntities = initialEntity.subentities;

for (NSEntityDescription *subEntity in subEntities) {
Class subEntityClass = NSClassFromString([subEntity managedObjectClassName]);

if ([subEntityClass respondsToSelector:@selector(shouldUseSubEntityRecordClassToRepresentData:)]) {
if ([subEntityClass shouldUseSubEntityRecordClassToRepresentData:object]) {
return [self subEntityForRecordResponseObject:object withInitialEntity:subEntity];
}
}
}
return initialEntity;
}

#pragma mark - Building Proto Records

- (void)buildProtoRecordsAndResponseGroups {
NSMutableDictionary *responseGroups = [NSMutableDictionary dictionary];
NSMutableArray *objectGraph = [NSMutableArray array];

NSArray *subEntities = self.initialEntity.subentities;


for (id recordResponseObject in self.responseObjectArray) {
NSEntityDescription *entity = self.initialEntity;

for (NSEntityDescription *subEntity in subEntities) {
Class subEntityClass = NSClassFromString([subEntity managedObjectClassName]);

if ([subEntityClass respondsToSelector:@selector(shouldUseSubEntityRecordClassToRepresentData:)]) {
if ([subEntityClass shouldUseSubEntityRecordClassToRepresentData:recordResponseObject]) {
entity = subEntity;
break;
}
}
}
NSEntityDescription *entity = [self subEntityForRecordResponseObject:recordResponseObject
withInitialEntity:self.initialEntity];

MMRecordProtoRecord *proto = [self protoRecordWithRecordResponseObject:recordResponseObject
entity:entity
Expand Down Expand Up @@ -277,8 +283,12 @@ - (void)addRelationshipProtoRecordsToProtoRecord:(MMRecordProtoRecord *)protoRec

for (id object in relationshipObject) {
if ([protoRecord canAccomodateAdditionalProtoRecordForRelationshipDescription:relationshipDescription]) {

NSEntityDescription *recordSubEntity = [self subEntityForRecordResponseObject:object
withInitialEntity:entity];

MMRecordProtoRecord *relationshipProto = [self protoRecordWithRecordResponseObject:object
entity:entity
entity:recordSubEntity
existingResponseGroups:responseGroups
parentProtoRecord:protoRecord];

Expand Down