我在Web服务上发布了一个表示商店ID的字符串,我正在按以下格式接收数据:
{
"status": {
"code": 201,
"message": "Session created"
},
"session": "a1e0f68e82ca4d0095d4b2a9582c7e21",
"store": {
"id": 62,
"code": "123",
"name": "ABCD",
"address1": "address line 1",
"address2": "address line 2",
"currencyCode": "USD",
},
"employees": [
{
...
}
]
}
我已经传递了一些响应描述符,如下所示:
对于会话对象:
RKObjectMapping *sessionMapping = [RKObjectMapping mappingForClass:[Session class]];
[sessionMapping addAttributeMappingsFromDictionary:@{@"session" : @"token"}];
RKResponseDescriptor *sessionDescriptor = [RKResponseDescriptor responseDescriptorWithMapping:sessionMapping method:RKRequestMethodAny pathPattern:nil keyPath:nil statusCodes:[NSIndexSet indexSetWithIndex:201]];
对于商店对象:
RKEntityMapping *storeMapping = [RKEntityMapping mappingForEntityForName:@"Store"
inManagedObjectStore:[[DataModel sharedDataModel] objectStore]];
[storeMapping addAttributeMappingsFromDictionary:@{@"code" : @"code", @"id" : @"uuid", @"name" : @"name", @"address1" : @"address1", @"address2" : @"address2", @"currencyCode" : @"currencyCode"}];
storeMapping.identificationAttributes = @[@"code"];
storeMapping.deletionPredicate = [NSPredicate predicateWithFormat:@"deleted = true || open = false"];
RKResponseDescriptor *storeDescriptor = [RKResponseDescriptor responseDescriptorWithMapping:storeMapping method:RKRequestMethodGET pathPattern:nil keyPath:nil statusCodes:RKStatusCodeIndexSetForClass(RKStatusCodeClassSuccessful)];
与员工对象类似。
这就是我运行操作的方式:
[[RKObjectManager sharedManager] addResponseDescriptorsFromArray:@[sessionDescriptor, storeDescriptor, usersDescriptor]];
RKObjectRequestOperation *operation = [[RKObjectManager sharedManager] appropriateObjectRequestOperationWithObject:nil method:RKRequestMethodPOST path:myPath parameters:parameters];
operation.targetObject = nil; //as per https://github.com/RestKit/RestKit/wiki/Object-mapping
[operation setCompletionBlockWithSuccess:^(RKObjectRequestOperation *operation, RKMappingResult *mappingResult)
{
successBlock(mappingResult);
} failure:nil];
[[RKObjectManager sharedManager] enqueueObjectRequestOperation:operation];
问题是虽然Session和Employees构建得很好,但我无法显示商店对象。什么都没有映射!
此外,商店实际上已经全部坚持作为前一次调用的对象,但他们只有一个代码和一个名字。此调用应该填充剩余的字段。
我的问题的第二部分显然在响应中,员工和商店都是根级对象。商店对象还将员工作为财产。所以我需要将employees数组中返回的内容映射到商店对象。有没有办法用描述符执行此操作,还是需要在成功完成块中手动执行此操作?
答案 0 :(得分:1)
您的商店响应描述符的密钥路径应设置为store
。此外,响应描述符链接到RKRequestMethodGET
方法,但您使用RKRequestMethodPOST
发出服务器请求。
请注意,您可能不应在谓词中使用deleted
(请注意命名与托管对象的冲突)。
查看foreign key mapping以连接非嵌套内容之间的关系。