假设我有一个这样的 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/