ios - Restkit 0.20 JSON 映射以及额外的离线数据

标签 ios objective-c restkit restkit-0.20

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

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

我能够将此对象映射到我的 user 类中,该类具有以下属性:

ID,
firstName,
lastName,
createdDate,
modifiedData

当我需要更新 modified date 时,问题就出现了 我希望在离线模式下进行映射以及修改数据时能够插入数据时间戳。

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

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

我的映射函数,如果有帮助的话:

+ (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 触发的方式(以及任何其他更改):

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

关于ios - Restkit 0.20 JSON 映射以及额外的离线数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21388797/

相关文章:

ios - 如何在 iOS 中获取 arctan MATH 函数?

ios - 不调用 FlurryAdBanner 中的委托(delegate)方法

ios6 - 如何在 RestKit v0.20.0 中设置 serviceUnavailableAlertEnabled

ios - RKObjectManager 进行 PUT 的正确用法是什么?

ios - 如何使用带有ID的RKResponseDescriptor?

ios - 如何在 UIStackView 中等距放置标签?

ios - 错误 : perhaps the designated entry point is not set?

ios - 如何保存多个对象,在 Realm 中为每个项目创建不同的行

objective-c - 应用内购买不再有效

ios - 如何处理 Swift 中缺少反射的问题?