Restkit 0.20 JSON映射以及其他脱机数据 [英] Restkit 0.20 JSON Mapping along with additional offline data

查看:116
本文介绍了Restkit 0.20 JSON映射以及其他脱机数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

所以假设我有一个像这样的JSON对象:

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.

所以我的问题是,如何将JSON对象映射到Core Data,同时还插入一些JSON对象中不存在的数据。这甚至可能吗?

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


推荐答案

处理RestKit外部,但是以一种由RestKit(以及任何其他更改)触发的方式:

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

在您的托管对象子类上覆盖 willSave 并在调用时更新修改日期(设置原始值以避免递归)。

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

这篇关于Restkit 0.20 JSON映射以及其他脱机数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆