Restkit 0.20 JSON Mapping along with additional of

2019-01-20 02:53发布

问题:

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).