Restkit 0.20 JSON Mapping along with additional offline data

不想你离开。 提交于 2019-11-27 07:12:57

问题


So let's say I have a JSON object like such:

{
  "userList" : [
    {
      "ID" : 1,
      "firstName" : "John",
      "lastName" : "Doe"
    },
    {
      "ID" : 2,
      "firstName" : "Jane",
      "lastName" : "Doe"
    }
  ]
}

I am able to map this object into my user class which have the following attribute:

ID,
firstName,
lastName,
createdDate,
modifiedData

The Problem arise when I am need to update modified date I want to be able to insert a data-time stamp whenever I do a mapping along with when I modified the data while in offline mode.

So my question is, how do I map JSON object to Core Data while also inserting some data that is not present in the JSON object. Is this even possible?

================

My Mapping Function, if it helps:

+ (RKObjectMapping *)mapping {
    // Create a singleton instance of the mapping object.
    __strong static RKEntityMapping *_mapping = nil;
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        RKManagedObjectStore *store = [[RKObjectManager sharedManager] managedObjectStore];
        _mapping = [RKEntityMapping mappingForEntityForName:NSStringFromClass([self class]) inManagedObjectStore:store];

        // Map attributes with the same name in the JSON and the model.
        [_mapping addAttributeMappingsFromDictionary:@{@"ID": @"ID",
                                                       @"firstName" : @"firstName",
                                                       @"lastName" : @"lastName"}];

        // Set primaryKeyAttribute
        _mapping.identificationAttributes = @[@"ID"];
    });
    return _mapping;
}

回答1:


Handle this outside RestKit, but in a way that is triggered by RestKit (and any other change):

Override willSave on your managed object subclass and update the modified date whenever it's called (setting the primitive value to avoid recursion).



来源:https://stackoverflow.com/questions/21388797/restkit-0-20-json-mapping-along-with-additional-offline-data

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