【发布时间】:2014-02-13 01:49:29
【问题描述】:
过去几个小时,我通过查看 StackOverFlow 线程上已解决的类似问题,尝试了许多不同的解决方案。
但是,我碰了壁。
一些背景。
使用 Restkit 0.22 和 Core Data 来持久化 2 个对象并在它们之间建立关系。 我正在建模的两个对象如下,关系也包含在 COAgendaItem 和 COMapItemLocation 之间的一对一关系(目前不起作用的关系)和 COMapItemLocation 和 COAgendaItem 之间的一对多关系(我还没看过映射这个):
-- COAgendaItem
-- COMapItemLocation
它们具有相应的属性和关系
期望的结果是让核心数据关系与 RestKit 一起工作,即通过 COAgendaItem 对象上的代码可访问属性议程项目位置。
来自服务器的 JSON 响应如下所示
上面的 JSON 响应是 COAgendaItem 对象加上 COMapItemLocation 对象的主键所需的数据。我在映射作为 COMapItemLocation 主键的 location_id 字段时遇到问题。 (我已经设置了响应描述符和检索代码并且它工作正常,自从我尝试映射关系后它才成为一个问题)
到目前为止,我已经修改了我的 RKEntityMapping 以尝试映射关系。
+ (RKMapping *)agendaItemMapping{
RKEntityMapping *agendaItemMapping = [RKEntityMapping mappingForEntityForName:@"COAgendaItem" inManagedObjectStore:[[COPersistenceCoordinator sharedCOPersistenceCoordinator]restKitObjectStore]];
[agendaItemMapping addAttributeMappingsFromDictionary:@{@"id": @"agendaItemID",@"title": @"agendaItemTitle",@"description": @"agendaItemDescription",@"start": @"agendaItemStartTime",@"end": @"agendaItemEndTime",@"category": @"agendaItemCategory",@"location_id" : @"agendaItemLocation"}];
[agendaItemMapping addConnectionForRelationship:@"agendaItemLocation" connectedBy:@"location_id"];
return agendaItemMapping;
}
但是这会导致启动时崩溃,如下所示(注意,我在默认映射中也为外键添加了 @"location_id" : @"agendaItemLocation" 映射,这是必要的)
我试过修改[agendaItemMapping addConnectionForRelationship:@"agendaItemLocation" connectedBy:@"location_id"];
包含我能想到的单个字符串和字典格式的所有组合的语句,即 @{"agendaItemLocation" : @"location_id"} 但无论我尝试什么,它总是以与之前相同的错误“无法连接关系:无效属性”结束为源实体“COAgendaItem”提供:location_id。
我完全被这个难住了,谁能指出我做错了什么或需要做不同的事情?
非常感谢您提前提供的任何帮助! :)
【问题讨论】:
标签: ios core-data restkit foreign-key-relationship