Core数据关系未使用RESTkit映射 [英] Core Data relationships not mapped with RESTkit

查看:142
本文介绍了Core数据关系未使用RESTkit映射的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



我想映射这个JSON:

  {items:[{id:2001,itemAttr1:...},...< more items> ; ...],
rooms:[{id:3001,items:[2001,...< more item id's> ...]}

到对应的Core Data模型:

  Entity ItemMO(属性id,itemAttr1,关系room到RoomMO)
Entity RoomMO(属性id,关系items到ItemMO)



属性映射正常,但关系为空。



使用 RKConnectionDescription (如此处,使用此代码:

  NSEntityDescription * roomEntity = [NSEntityDescription entityForName:@RoomMOinManagedObjectContext:self.context]; 
NSRelationshipDescription * itemsInRoom = [roomEntity relationshipsByName] [@items];
RKConnectionDescription * connection = [[RKConnectionDescription alloc] initWithRelationship:devicesInRoom keyPath:@devices];
[roomMapping addConnection:connection];我也尝试过使用一个简单的 RKRelationshipMapping



<无效:

  [itemMapping addPropertyMapping:[RKRelationshipMapping relationshipMappingFromKeyPath:nil toKeyPath:@roomwithMapping:roomMapping]]; 

我必须缺少一些简单的东西,因为这不应该是RESTkit的异常情况。任何想法?

我得到它的工作。
诀窍是在ItemMO上为外键添加一个额外的属性'roomId'。

 实体ItemMO id,roomId,itemAttr1,Relationshiproomto RoomMO)

关于关系的RESTkit:

  [itemMapping addConnectionForRelationship:@roomconnectedBy:@ {@roomId:@id }]; 

似乎RESTkit无法建立没有额外外键属性的关系。


I am struggling to get a proper mapping of Core Data relationships using RESTkit 0.20.0-pre6.

I want to map this JSON:

{ "items" : [ {"id" : 2001, "itemAttr1" : "..."}, ...<more items>... ],
  "rooms":  [ {"id" : 3001, "items": [2001, ...<more item id's>...] }

to a corresponding Core Data model:

Entity ItemMO (Attributes "id", "itemAttr1", Relationship "room" to RoomMO)
Entity RoomMO (Attributes "id", Relationship "items" to ItemMO)

The attributes are mapped fine, but the relationships are empty.

I have tried using RKConnectionDescription as described here, using this code:

NSEntityDescription *roomEntity = [NSEntityDescription entityForName:@"RoomMO" inManagedObjectContext:self.context];
NSRelationshipDescription *itemsInRoom = [roomEntity relationshipsByName][@"items"];
RKConnectionDescription *connection = [[RKConnectionDescription alloc] initWithRelationship:devicesInRoom keyPath:@"devices"];
[roomMapping addConnection:connection];

I have also tried using a simple RKRelationshipMapping to no avail:

[itemMapping addPropertyMapping:[RKRelationshipMapping relationshipMappingFromKeyPath:nil toKeyPath:@"room" withMapping:roomMapping]];

I must be missing something simple as this should not be an exotic case for RESTkit. Any ideas?

解决方案

I got it working. The trick was to add an extra attribute 'roomId' on ItemMO for the foreign key.

Entity ItemMO (Attributes "id", "roomId", "itemAttr1", Relationship "room" to RoomMO)

Then tell RESTkit about the relation:

[itemMapping addConnectionForRelationship:@"room" connectedBy:@{@"roomId" : @"id"}];

It seems as if RESTkit cannot establish the relation without the extra foreign key attribute.

这篇关于Core数据关系未使用RESTkit映射的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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