使用RestKit 0.10.1,我的对象服务类似于这种json格式:
{"objects": [
{"owner": 1,
"_id": 823,
"data": {
"diam": 5.0,
"plant_date": "10/02/2008"}
},
... ] }
在客户端,我不需要有子对象或关系,所以我扁平化我的对象的属性:
[myMapping mapKeyPathsToAttibutes:
@"_id", @"id",
@"owner", @"owner",
@"data.diam", @"diam", //here is what I mean by flatten; notice data.diam -> diam
@"data.plant_date", @"plant_date", nil];
我读取这些数据没有问题,但是当我想序列化它时,只有顶层属性被发送到服务器。当我序列化时,这是服务器获取的内容:
{"_id":0,"owner":1}
请注意,我已经正确地(我认为)使用上面的inverseMapping注册了序列化映射:
[objectManager.mappingProvider setSerializationMapping:[myMapping inverseMapping] forClass:[MyClass class]];
何时我发布了这样的对象:
myObject = [MyClass object];
myObject.diam = [NSNumber numberWithInt:5];
myObject.plant_date = myDate;
[[RKObjectManager sharedManager] postObject:myObject delegate:self];
我希望拥有完整,简洁的结构:
{"_id":0,"owner":1, "data": {"diam": 5.0, "plant_date": "10/02/2008"} }
如何使用RestKit发布将注册对象映射到服务器的密钥路径(即“data.diam”)?
答案 0 :(得分:3)
这看起来像是RestKit中的一个错误。您可以使用以下代码段重现它:
RKLogConfigureByName("RestKit/ObjectMapping", RKLogLevelTrace);
NSDictionary* src = [NSDictionary dictionaryWithObjectsAndKeys: @"cat", @"animal", nil];
NSMutableDictionary* dst = [NSMutableDictionary dictionary];
RKObjectMapping* mapping = [RKObjectMapping mappingForClass: [NSDictionary class]];
[mapping mapKeyPath: @"type.animal" toAttribute: @"animal"];
RKObjectMappingOperation* op = [[RKObjectMappingOperation alloc] initWithSourceObject: src
destinationObject: dst
mapping: [mapping inverseMapping]];
NSError* error = nil;
[op performMapping: &error];
如果查看日志,您会看到以下行:
restkit.object_mapping:RKObjectMappingOperation.m:258目的地 object {}拒绝了keyPath type.animal的属性值cat。 跳过...
失败的方法是-validateValue:forKeyPath:error:因为关系组件不存在。我建议你在RestKit github上打开一个错误/问题报告。
答案 1 :(得分:2)
请注意,此错误已在RestKit中针对0.20版本的开发进行了修复。请参阅变更集@ https://github.com/RestKit/RestKit/commit/64e9c7cb6d04dd8750e9f663fc998bbe738945e9