| 测试已测试 | ✓ |
| 语言语言 | Objective-CObjective C |
| 许可证 | MIT |
| 发布最后发布 | 2015年3月 |
由 Elvis Nuñez 维护。
这是一个在 NSManagedObject 上增加的类别,它通过比较您的 JSON 字典和您本地的 CoreData 对象来帮助您评估插入、删除和更新。它还为您的本地存储对象提供了唯一性。
+ (void)andy_mapChanges:(NSArray *)changes
inContext:(NSManagedObjectContext *)context
forEntityName:(NSString *)entityName
inserted:(void (^)(NSDictionary *objectDict))inserted
updated:(void (^)(NSDictionary *objectDict, NSManagedObject *object))updated;- (void)importObjects:(NSArray *)objects usingContext:(NSManagedObjectContext *)context error:(NSError *)error
{
[NSManagedObject andy_mapChanges:JSON
inContext:context
forEntityName:@"User"
inserted:^(NSDictionary *objectDict) {
ANDYUser *user = [ANDYUser insertInManagedObjectContext:context];
[user fillObjectWithAttributes:objectDict];
} updated:^(NSDictionary *objectDict, NSManagedObject *object) {
ANDYUser *user = (ANDYUser *)object;
[user fillObjectWithAttributes:objectDict];
}];
[context save:&error];
}localKey是本地primaryKey的名称,如果是一个用户,它可能是remoteID。remoteKey是JSON中键的名称,如果是一个用户,它可能是仅仅id。
这个便捷的方法不包含这些属性,对于localKey回退到remoteID,对于remoteKey回退到id。
使用谓词来过滤映射变更。例如,如果JSON响应只属于不活跃的用户,那么可以有如下的谓词
NSPredicate *predicate = [NSString stringWithFormat:@"inactive = YES"];作为一个旁注,您应该使用一个更复杂的属性映射器,它为您执行fillObjectWithAttributes部分。
要运行示例项目,克隆库,然后从 Demo 目录打开.xcodeproj。
iOS 7.0,Core Data
NSManagedObject-ANDYMapChanges通过CocoaPods提供。要安装它,只需将以下行添加到您的 Podfile
pod 'NSManagedObject-ANDYMapChanges'Elvis Nuñez,[email protected]
NSManagedObject-ANDYMapChanges根据MIT许可证提供。有关更多信息,请参阅LICENSE文件。