1

映射实体时如何设置静态值?

我有这样的 JSON 响应:

"friends": [
    {
        "id": 123,
        "name": "Friend",
    },
]
"featured": [
    {
        "id": 456,
        "name": "Some Featured user",
    },
]

我的映射和描述符如下所示:

RKMapping *friendsMapping = [ProjectMappingProvider userMapping];
RKMapping *featuredMapping = [ProjectMappingProvider featuredUserMapping];

RKResponseDescriptor *friendsResponseDescriptor = [RKResponseDescriptor responseDescriptorWithMapping:friendsMapping
        method:RKRequestMethodGET
        pathPattern:@"/api/users"
        keyPath:@"friends"
        statusCodes:statusCodeSet];

RKResponseDescriptor *featuredResponseDescriptor = [RKResponseDescriptor responseDescriptorWithMapping:friendsMapping
        method:RKRequestMethodGET
        pathPattern:@"/api/users"
        keyPath:@"featured"
        statusCodes:statusCodeSet];

RKManagedObjectRequestOperation *operation = [[RKManagedObjectRequestOperation alloc] initWithRequest:request
        responseDescriptors:@[
        friendsResponseDescriptor,
        featuredResponseDescriptor]];

... some code emited for readabilty ...

现在 mu friendsResponseDescriptor 和 featuresResponseDescriptors 看起来几乎相同,但我想相应地设置额外的 CoreData 参数。通过friendsDescriptor映射的对象应该有section = 0,通过特征描述符映射的对象应该有section = 10。

那么,我可以做这样的事情吗?

RKEntityMapping *mapping = [RKEntityMapping mappingForEntityForName:@"User"
                                               inManagedObjectStore:[[DataModel sharedDataModel] objectStore]];
[mapping addAttributeMappingsFromDictionary:@{
                                              @"id": @"userId",
                                              @"name": @"name"                                                  }];
mapping.identificationAttributes = @[ @"userId" ];

// How can I do somethning like this?
[mapping setValue:@0 forKey:@"section"];

以及特色地图:

RKEntityMapping *mapping = [RKEntityMapping mappingForEntityForName:@"User"
                                               inManagedObjectStore:[[DataModel sharedDataModel] objectStore]];
[mapping addAttributeMappingsFromDictionary:@{
                                              @"id": @"userId",
                                              @"name": @"name"                                                  }];
mapping.identificationAttributes = @[ @"userId" ];

// How can I do somethning like this?
[mapping setValue:@10 forKey:@"section"];

请注意,无论用户是朋友还是用户 JSON 本身中的特色,我都没有任何其他指标。我可以区分用户类型(朋友,精选)的唯一方法是在 JSON 响应中设置了用户的哪个列表。我稍后使用 table view 控制器中的 section 属性来拥有部分。

4

1 回答 1

0

如果您使用不同的实体,请在它们上设置默认值。如果您不使用不同的实体,请考虑进行更改,以便您使用(它们可能是共同父级的子实体)。

您不能将数据插入到映射中。该映射仅描述了 RestKit 应该处理的内容。要编辑值,您需要自己参与映射过程并实现一些委托方法来注入额外的数据。

于 2013-09-16T15:01:36.503 回答