【问题标题】:Set a static value in restkit mapping在restkit映射中设置一个静态值
【发布时间】:2013-09-20 15:43:59
【问题描述】:

映射实体时怎么不能设置静态值?

我有一个这样的 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 controller 中的 section 属性来拥有section。

【问题讨论】:

    标签: ios objective-c mapping restkit


    【解决方案1】:

    如果您使用不同的实体,请为其设置默认值。如果您不使用不同的实体,请考虑更改为您使用的实体(它们可能是共同父实体的子实体)。

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

    【讨论】:

    • 您能详细解释一下吗?实现委托方法以注入额外的数据。 Reskit 中的哪些方法可以做到这一点。举个例子就完美了。
    • 我没有例子,我从不想参与到那个级别。您需要自己运行映射操作。更好(当然更容易)是具有默认值的不同实体......
    猜你喜欢
    • 2013-07-31
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-05-03
    相关资源
    最近更新 更多