如何将展平的对象序列化回服务器,在RestKit中解压缩

使用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发布将注册对象映射到服务器的keypath(即“data.diam”)?

请注意,此错误已在RestKit中针对0.20版本的开发进行了修复。 请参阅更改集@ https://github.com/RestKit/RestKit/commit/64e9c7cb6d04dd8750e9f663fc998bbe738945e9

这看起来像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目标对象{}拒绝了keyPath type.animal的属性值cat。 跳绳…

失败的方法是-validateValue:forKeyPath:error:因为关系组件不存在。 我建议你在RestKit github上打开一个错误/问题报告。