RestKit:连接多对多关系 [英] RestKit: connecting many-to-many relationship

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

问题描述

我的应用程序具有以下核心数据模型:

I've got the following Core Data model is my app:

+--------------------+         +-------------+
| Ingredient         |         | Dish        |
+--------------------+         +-------------+
| id                 |         | id          |
| title              |         | title       |
| dishId (transient) |
+--------------------+         +-------------+
| dishes             | <<--->> | ingredients |
+--------------------+         +-------------+

我也有一个Web服务,可以让我请求像这样的给定菜肴的所有配料:

I also have a webservice, which allows me to request all ingredients for a given dish like this:

/dish/1/ingredients

(其中 1 是一些菜ID).

(where 1 is some dish id).

对此响应的请求如下:

{
    { "id": "1", "title": "milk" },
    { "id": "2", "title": "egg" },
    { "id": "3", "title": "flour" }
}

我已经阅读了RKConnectionDescription,但是在这种特殊情况下,我不知道如何使用它来连接这两个实体.

I've read about RKConnectionDescription, but I can't figure out how to use it to connect these two entities in this particular case.

这是到目前为止我得到的映射:

Here's the mapping I've got so far:

RKEntityMapping *mapping = [RKEntityMapping mappingForEntityForName:@"Ingredient" inManagedObjectStore:store];

[mapping setIdentificationAttributes:@[@"id"]];

[mapping addAttributeMappingsFromDictionary:@{
        @"id" : @"id",
        @"title" : @"title",
        @"@metadata.dishId" : @"dishId"
}];

更新

我向我的 Ingredient 实体添加了一个新的瞬态属性 dishId ,并尝试建立如下连接:

I've added a new transient property dishId to my Ingredient entity and tried to set up connection like this:

NSEntityDescription *entityDescription =
        [NSEntityDescription entityForName:@"Ingredient" inManagedObjectContext:mainContext];
NSRelationshipDescription *relationship =
        [entityDescription relationshipsByName][@"dishes"];
RKConnectionDescription *connection =
        [[RKConnectionDescription alloc] initWithRelationship:relationship attributes:@{
                @"dishId" : @"id"
        }];

[mapping addConnection:connection];

但是每次我要求另一道菜的食材时,它都会用新的食材代替旧的食材,因此我基本上得到了一对一的关系.例如,我首先请求煎蛋的原料,它将煎蛋与鸡蛋,牛奶和面粉连接起来.然后我请求煎饼的食材,它将煎饼与鸡蛋,牛奶和面粉连接起来,但是与这些食材的煎蛋卷连接断开了.

but each time I request ingredients for another dish, it replaces old connections with a new ones, so I basically get one-to-one relationship. For example, I first request ingredients for omelette, and it connects omelette to eggs, milk and flour. Then I request ingredients for pancakes, and it connects pancakes to eggs, milk and flour, however omelette connection to these ingredients is lost.

assignmentPolicy RKConnectionDescription 上不可用.

推荐答案

您要使用 RKRoute 可以创建请求路径并为您提供所需的关系映射(您不需要自己做).您需要将路由的路径模式设置为/dish/:id/ingredients ,以便从 Dish 中提取身份并进行注入.

You want to use RKRoute to create the request path and to provide you with the required relationship mapping (you don't need to do it yourself). You need to set the path pattern for the route to /dish/:id/ingredients so that the identity is pulled from the Dish and injected.

使用 routeWithRelationshipName:objectClass:pathPattern:method:创建路由.

使用 getObjectsAtPathForRelationship:ofObject:parameters:success:failure:来获取内容.

一旦连接了路由,URL内容元数据将提供它所做的任何映射.因此,在映射中,您可以使用:

Once you have the route connected, any mapping that it does will be provided with the URL content metadata. So, in your mapping you use:

@"@metadata.routing.parameters.id" : @"dishId"

请注意, dishId 是添加到 Ingredient 中的新瞬态属性,可用于外键映射( RKConnectionDescription ).

Note that dishId is a new transient attribute added to Ingredient that you use in your foreign key mapping (RKConnectionDescription).

这篇关于RestKit:连接多对多关系的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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