How to serialize a flattened object back to server, unflattened in RestKit

对着背影说爱祢 提交于 2019-12-24 00:50:10

问题


Using RestKit 0.10.1, I have objects served similar to this json format:

{"objects": [
    {"owner": 1, 
     "_id": 823, 
     "data": {
         "diam": 5.0, 
         "plant_date": "10/02/2008"}
    }, 
     ...   ] }

on the client side, I do not need to have sub objects or relationships, so I flatten to attributes of my object:

[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];

I have no problem reading this data, but when I want to serialize it, only the top-level attributes get sent to the server. When I serialize, here is what the server gets:

{"_id":0,"owner":1}

Note that I have correctly (I think) registered the Serialization mapping with an inverseMapping of the above:

[objectManager.mappingProvider setSerializationMapping:[myMapping inverseMapping] forClass:[MyClass class]];

When I post the object like so:

myObject = [MyClass object];
myObject.diam = [NSNumber numberWithInt:5];
myObject.plant_date = myDate;

[[RKObjectManager sharedManager] postObject:myObject delegate:self];

I would like to have the complete, unflattened structure:

{"_id":0,"owner":1, "data": {"diam": 5.0, "plant_date": "10/02/2008"} }

How can I achieve posting a keypath (i.e. "data.diam") mapping registered object to server using RestKit?


回答1:


Note that this bug has been fixed in RestKit on the development for release in 0.20. See the changeset @ https://github.com/RestKit/RestKit/commit/64e9c7cb6d04dd8750e9f663fc998bbe738945e9




回答2:


This looks like a bug in RestKit. You can reproduce it with the following snippet:

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];

If you look at the log you will see this line:

restkit.object_mapping:RKObjectMappingOperation.m:258 Destination object { } rejected attribute value cat for keyPath type.animal. Skipping...

The method that fails is -validateValue:forKeyPath:error: because the relationship component does not exist. I would suggest you open a bug/issue report on the RestKit github.



来源:https://stackoverflow.com/questions/11497335/how-to-serialize-a-flattened-object-back-to-server-unflattened-in-restkit

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!