RestKit - 到同一实体的关系映射创建无限循环
Posted
技术标签:
【中文标题】RestKit - 到同一实体的关系映射创建无限循环【英文标题】:RestKit - Relationship mapping to same entity creates infine loop 【发布时间】:2014-03-31 06:12:50 【问题描述】:我需要创建一个实体与同一个实体的关系。基本上它是Meetings
实体,可能有任何ParantMeeting
。
这就是我建立关系的方式。
- (RKEntityMapping *)meetingsMapping
RKEntityMapping *meetingsMapping = [RKEntityMapping mappingForEntityForName:@"DBMeetings" inManagedObjectStore:objectManager.managedObjectStore];
meetingsMapping.setDefaultValueForMissingAttributes = NO;
meetingsMapping.deletionPredicate = [NSPredicate predicateWithFormat:@"shouldBeDeleted = 1"];
[meetingsMapping setModificationAttributeForName:@"updated_at"];
meetingsMapping.identificationAttributes = @[@"id"];
[meetingsMapping addAttributeMappingsFromDictionary:@
@"id": @"id",
@"title": @"title",
@"start_time": @"start_time",
@"finish_time": @"finish_time",
@"lock": @"lock",
@"location": @"location",
@"sample": @"sample",
@"deleted": @"shouldBeDeleted",
@"created_at": @"created_at",
@"updated_at": @"updated_at",
@"follow_up_id": @"follow_up_id",
@"total_topics": @"total_topics",
];
[meetingsMapping addRelationshipMappingWithSourceKeyPath:@"tags" mapping:[self tagsMapping]];
[meetingsMapping addRelationshipMappingWithSourceKeyPath:@"required_participants" mapping:[self contactsMapping]];
[meetingsMapping addRelationshipMappingWithSourceKeyPath:@"optional_participants" mapping:[self contactsMapping]];
[meetingsMapping addRelationshipMappingWithSourceKeyPath:@"readonly_participants" mapping:[self contactsMapping]];
[meetingsMapping addRelationshipMappingWithSourceKeyPath:@"organizer" mapping:[self contactsMapping]];
[meetingsMapping addRelationshipMappingWithSourceKeyPath:@"parent_meeting" mapping:[self meetingsMapping]];
return meetingsMapping;
每当我为parent_meeting
添加关系映射时
[meetingsMapping addRelationshipMappingWithSourceKeyPath:@"parent_meeting" mapping:[self meetingsMapping]];
它创建了一个无限循环。是否有任何其他方式可以创建与同一实体的关系。
请帮忙。
【问题讨论】:
【参考方案1】:替换引起递归的行:
[meetingsMapping addRelationshipMappingWithSourceKeyPath:@"parent_meeting" mapping:[self meetingsMapping]];
直接引用映射本身:
[meetingsMapping addRelationshipMappingWithSourceKeyPath:@"parent_meeting" mapping:meetingsMapping];
【讨论】:
感谢您的回复,它成功了。它是否记录在某处?我找不到。 映射能够引用自身?我记得没有具体记录。 我以前做过。递归关系可以通过递归映射来实现,这似乎是合乎逻辑的...... 是的,你是对的,我也认为“递归关系可以通过递归映射来实现”,但我猜不出我应该使用meetingsMapping
而不是[self meetingsMapping]
。 :)
通过调用 [self meetingsMapping]
您并没有创建递归映射 - 您正在创建一个永无止境的映射链(因此使用递归调用而不是递归引用)。以上是关于RestKit - 到同一实体的关系映射创建无限循环的主要内容,如果未能解决你的问题,请参考以下文章
Restkit/Core Data 关系映射,实体到相同类型的实体(父/子)