映射实体时如何设置静态值?
我有这样的 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 属性来拥有部分。