Restkit - 通过外键的核心数据一对一关系映射 [英] Restkit - Core Data One-To-One Relationship Mapping via Foreign Key

查看:186
本文介绍了Restkit - 通过外键的核心数据一对一关系映射的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



我已经花了过去几个小时,尝试了一些不同的解决方案这个问题,通过查找类似的类似的问题,已解决在StackOverFlow线程。



使用Restkit 0.22和Core Data保留2个对象并建立他们之间的关系。
我建模的两个对象如下,关系也包括在COAgendaItem和COMapItemLocation(目前不工作的关系)之间的一对一关系的形式和一个多对象在COMapItemLocation和COAgendaItem(我还没有查看映射):



- COAgendaItem





- COMapItemLocation



>



他们有相应的属性和关系



所需的结果是使核心数据关系与RestKit一起工作,即通过COAgendaItem对象上的代码可访问属性agendaItemLocation。



来自服务器的JSON响应如下所示





上述JSON响应是COAgendaItem对象所需的数据加上COMapItemLocation对象的主键。我有一个问题映射location_id字段是COMapItemLocation的主键。 (我有响应描述符和检索代码设置,它正常工作,它只是成为一个问题,因为我试图映射关系)



到目前为止我已修改我的RKEntityMapping以尝试并映射关系。

  +(RKMapping *)agendaItemMapping {

RKEntityMapping * agendaItemMapping = [RKEntityMapping mappingForEntityForName:@COAgendaIteminManagedObjectStore:[[COPersistenceCoordinator sharedCOPersistenceCoordinator] restKitObjectStore]];
[agendaItemMapping addAttributeMappingsFromDictionary:@ {@id:@agendaItemID,@title:@agendaItemTitle,@description:@agendaItemDescription,@start:@agendaItemStartTime @end:@agendaItemEndTime,@category:@agendaItemCategory,@location_id:@agendaItemLocation}];
[agendaItemMapping addConnectionForRelationship:@agendaItemLocationconnectedBy:@location_id];


return agendaItemMapping;

}



在启动时如下所示(注意,我已经为默认映射中的外键添加了@location_id:@agendaItemLocation映射,这是必要的)





我试过修改 [agendaItemMapping addConnectionForRelationship:@agendaItemLocationconnectedBy:@location_id];

所有的组合,我可以想到的单字符串和字典格式,即@ {agendaItemLocation:@location_id}的语句,但无论我试试,结束了与之前相同的错误'无法连接关系:为源实体给定的无效属性'COAgendaItem':location_id'。



我完全无法在这一个,任何人指出我做错了或需要做不同的东西?



非常感谢任何帮助提前! :)

解决方案

你描述了两种不同的关系,但实际上你只有一个具有逆。这是正确的,它只是你的描述是错误的(因此可能你的理解设置关系内容)。



连接不能做出来, ),因为RestKit需要知道要找到的其他对象的ID(连接到)。你要告诉它使用 location_id ,但它需要是类的属性(通常是transient),而不是JSON中的项目(因为连接是在



向实体添加属性,并将映射添加到 location_id 进去。使用该属性的名称更新连接。


I have spent the last few hours trying a number of different solutions to this problem by looking at similar similar problems that have been resolved on StackOverFlow threads.

However, I have hit a brick wall.

Some background.

Using Restkit 0.22 and Core Data to Persist 2 Objects and set up a relationship between them. The two Objects that I am modelling are as follows, Relationships are also included in the form of a One-One relationship between COAgendaItem and COMapItemLocation (The relationship that isn't working at present) and a One-Many between COMapItemLocation and COAgendaItem (I have not looked at mapping this yet):

-- COAgendaItem

-- COMapItemLocation

They have the Corresponding Attributes and Relationships

The desired result is to have the core data relationship working alongside RestKit, i.e. The property agendaItemLocation accessible via code on a COAgendaItem object.

The JSON Response from the Server looks like this

The above JSON Response is the data required for a COAgendaItem object plus the primary key of a COMapItemLocation Object. I am having a problem mapping the location_id field which is the primary key of a COMapItemLocation. (I have the Response Descriptors and the retrieval code set up and it is working correctly, it has only become a problem since I attempted to map a relationship)

So far I have modified my RKEntityMapping to try and map the relationship.

+ (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;

}

However this causes a crash on launch as shown below (Note, I have added the @"location_id" : @"agendaItemLocation" mapping for the foreign key in the default mapping as well, is this necessary)

I have tried modifying the [agendaItemMapping addConnectionForRelationship:@"agendaItemLocation" connectedBy:@"location_id"]; Statement with all the combinations I can think of both in single string and dictionary formats i.e. @{"agendaItemLocation" : @"location_id"} but whatever I try, it always ends up in the same error as before 'Cannot connect relationship: invalid attributes given for source entity 'COAgendaItem': location_id'.

I am completely stumped on this one, can anyone point anything that I have done wrong or need to do differently?

Thanks greatly for any help in advance! :)

解决方案

You describe 2 different relationships, but really you just have one with an inverse. This is correct, it's just your description which is wrong (and thus probably your understanding of setting the relationship contents).

The connection cannot be made (and you get an error) because RestKit needs to know the id of the other object to find (to connect to). You're telling it to use location_id, but it needs to be a property on the class (usually transient), not an item in the JSON (because the connection is made after the JSON has been processed and gone).

Add a property to the entity and a mapping to place the location_id into it. Update the connection with the name of that property.

这篇关于Restkit - 通过外键的核心数据一对一关系映射的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆