向RestKit映射添加一对一外键连接的问题 [英] Issue with adding a one-to-one foreign-key connection to a RestKit mapping

查看:123
本文介绍了向RestKit映射添加一对一外键连接的问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



我的JSON如下:

  {
父母:{
标记:标记
标题:标题,
描述:描述,
child_id:1
}
}

Parent ManagedObject在下有一个 Child 关键(和相反)。在CoreData中已经有一个对应的 Child 对象后, Parent 映射就发生了,所以这不是问题。 >

为了解决这个问题,我在网络上进行了一些链接和讨论,但都没有成功。什么是正确的方法来做到这一点,而不必添加 child_id 属性到父母(有些人声称他们做了这个,但它看起来不对我)。

假设我有这个映射:

pre $ $ code> - (RKEntityMapping *)parentResponseMapping {
RKEntityMapping * mapping = [RKEntityMapping mappingForEntityForName:@ParentinManagedObjectStore:[self myStore]];
[mapping addAttributeMappingsFromArray:@ [@title,@description]];
mapping.identificationAttributes = @ [@token];
返回映射;
}

起初我尝试添加这样的连接:

  NSEntityDescription * parentEntity = [NSEntityDescription entityForName:@ParentinManagedObjectContext:[self myContext]]; 
NSRelationshipDescription * childRelationship = [parentEntity relationshipsByName] [@seller];
RKConnectionDescription * connection = [[RKConnectionDescription alloc] initWithRelationship:childRelationship keyPath:@child_id];

[mapping addConnection:connection];

但是我得到一个错误, child_id 是不是一个有效的属性(这是我尝试添加 child_id 属性到我的方案,但又一次,这似乎混乱,而且,它不工作)。 / p>

我也尝试通过添加连接:

  [addConnectionForRelationship:@ childconnectedBy:@child_id] 

但是这也行不通。



这两个方法都会忽略子映射。

什么是方法实现这是什么?



编辑 RestKit对象映射与外键的关系 - 这已经在这里得到了回答,但解决方案看起来很奇怪。这是做到这一点的唯一方法吗?

解决方案


不必向child添加一个child_id属性(有些人声称他们这样做了,但是这对我来说是错误的)。

这实际上就是 right 方法做到这一点......



关键是RestKit需要一些方法将两个项目映射在一起,一些关键字用来搜索和查找要连接的项目。这个信息只需要是暂时的(假设你一次加载数据或者识别的另一侧是持久属性)。没有其他办法可以做到。


I am trying to add a connection to a RestKit mapping but without success.

My JSON is as follows:

{
  parent: {
    token: "token"
    title: "title",
    description: "description",
    child_id: 1
  }
}

My CoreData scheme defines that the Parent ManagedObject has a Child relation under the child key (and the opposite). The Parent mapping happens after I already have a corresponding Child object in CoreData, so that's not a problem.

I followed some links and discussions around the web in order to solve this, but none work. What is the right way to do this, without having to add a child_id property to Parent (some people claimed they did this but it looks wrong to me).

Assuming I have this mapping:

- (RKEntityMapping *)parentResponseMapping {
    RKEntityMapping *mapping = [RKEntityMapping mappingForEntityForName:@"Parent" inManagedObjectStore:[self myStore]];
    [mapping addAttributeMappingsFromArray:@[@"title", @"description"]];
    mapping.identificationAttributes = @[@"token"];
    return mapping;
}

At first I tried adding the connection like this:

NSEntityDescription *parentEntity = [NSEntityDescription entityForName:@"Parent" inManagedObjectContext:[self myContext]];
NSRelationshipDescription *childRelationship = [parentEntity relationshipsByName][@"seller"];
RKConnectionDescription *connection = [[RKConnectionDescription alloc] initWithRelationship:childRelationship keyPath:@"child_id"];

[mapping addConnection:connection];

But I got an error that child_id is not a valid attribute (this is where I tried adding the child_id property to my scheme but again, that seems messy to me, and besides, it didn't work).

I also tried adding the connection by:

[addConnectionForRelationship:@"child" connectedBy:@"child_id"]

But this didn't work either.

Both these methods ignore the child mapping anyway.

What is the right way to achieve this?

EDIT: RestKit Object Mapping relationships with foreign keys - This was answered here already but the solution looks odd to me. Is this the only way to do this?

解决方案

without having to add a child_id property to Parent (some people claimed they did this but it looks wrong to me).

This is actually the right way to do it...

The point is the RestKit needs some way to map the two items together, some key with which to search and find the items to connect. This information only needs to be transient (assuming you are loading the data in one go or the other side of the identification is a persistent attribute). There isn't really another way to do it.

这篇关于向RestKit映射添加一对一外键连接的问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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