Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
317 views
in Technique[技术] by (71.8m points)

ios - Restkit 0.20 JSON Mapping along with additional offline data

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;
}
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...