测试已测试 | ✓ |
Lang语言 | Obj-CObjective C |
许可证 | MIT |
发布最新发布 | 2015年10月 |
由Ievgen Rudenko和Malovichko Oleksandr维护。
Core Data 构模代码的终结者。
pod 'MAGCoreData', '~> 0.0.4'
MAGCoreData 设置
[MAGCoreData prepareCoreData];
[MAGCoreData instance].autoMergeFromChildContexts = YES;
您还可以使用以下设置调用之一,与 MAGCoreData 类一起初始化
+ (BOOL)prepareCoreDataWithModelName:(NSString *)modelName error:(NSError **)error;
+ (BOOL)prepareCoreDataWithModelName:(NSString *)modelName andStorageName:(NSString *)storageName error:(NSError **)error;
要访问默认上下文,可以调用
NSManagedObjectContext *defaultContext = MAGCoreData.context;
如果您需要为使用在非主线程中创建新的托管对象上下文,可以使用以下方法
NSManagedObjectContext *privateContext = MAGCoreData.createPrivateContext;
要在默认上下文中创建并插入实体的新实例,可以使用
Weather *weather = [Weather create];
Weather *weather = [Weather createFromDictionary:dictionary];
您也可以创建并插入特定上下文中的对象
Weather *weather = [Weather createInContext:context];
Weather *weather = [Weather createFromDictionary:dictionary inContext:context];
要更新对象,应指定主键。有关其他说明,请参阅“映射”部分。
要更新或创建新对象
Weather *weather = [Weather safeCreateOrUpdateWithDictionary:dictionary];
要更新或创建特定上下文中的对象
Weather *weather = [Weather safeCreateOrUpdateWithDictionary:dictionary inContext:context];
要更新对象,更改对象的属性,主键值可能会更新
[weather safeSetValuesForKeysWithDictionary:dictionary];
Weather *weather = [Weather objectForPrimaryKey:primaryKey];
Weather *weather = [Weather getOrCreateObjectForPrimaryKey:primaryKey];
Weather *weather = [Weather first];
...
NSArray *array = [Weather all];
NSArray *array = [Weather allOrderedBy:@"temperature" ascending:YES];
NSArray *array = [Weather allForPredicate:predicate orderBy:@"temperature" ascending:YES];
...
您还可以在特定上下文中使用这些调用中的任何一个。
您可能应该保存任何所做的更改后的数据,因为如果应用程序崩溃,您将会丢失所有更改。
要保存数据
[MAGCoreData save];
或
[MAGCoreData saveContext:context];
在默认上下文中删除单个对象
[weatherObject delete];
在默认上下文中删除所有对象
[Weather deleteAll];
在特定上下文中删除所有对象
[Weather deleteAllInContext:context];
使用示例
#import "Weather.h"
#import "NSManagedObject+MAGCoreData.h"
@interface Weather ()
@end
@implementation Weather
+ (void)initialize {
[self setKeyMapping:@{
@"identifier" : @"id",
@"city" : @"city",
@"temperature" : @"temperature",
@"updatedAt" : @"updatedAt"
}];
[self setPrimaryKeyName:@"identifier"];
[self setUpdateDateKeyName:@"updatedAt"];
[self setDateFormats:@{@"updatedAt":@"yyyy'-'MM'-'dd'T'HH':'mm':'ss.SSS'Z'"}];
}
@end
Weather *weather = [Weather createFromDictionary:@{@"id": @"1", @"city": @"Glasgow", @"temperature": @"17"}];
NSLog(@"%@", weather.identifier); // 1
NSLog(@"%@", weather.city); // Glasgow
NSLog(@"%@", weather.temperature); // 17
该功能允许如果键字典和对象的属性名称相同,则自动设置属性。
Student *student = [Student createFromDictionary:@{@"identifier": @"1", @"name": @"Marcus"}];
NSLog(@"id = %@", student.identifier); // 1
NSLog(@"name = %@", student.name); // Marcus
使用示例
@implementation School
+ (void)initialize {
[self setKeyMapping:@{@"identifier": @"id", @"students": @"students"}];
[self setPrimaryKeyName:@"identifier"];
[self setRelationClasses:@{@"students": [Student class]}];
}
@end
@implementation Student
+ (void)initialize {
[self setKeyMapping:@{@"identifier": @"id", @"name": @"name"}];
[self setPrimaryKeyName:@"identifier"];
}
@end
NSDictionary *dictionary = @{@"id": @"1", @"students": @[@{@"id": @"1", @"name": @"Marcus"}, @{@"id": @"2", @"name": @"Livia"}]};
School *school = [School createFromDictionary:dictionary];
NSLog(@"First student's name is %@", ((Student *)school.students.allObjects[0]).name); // Marcus
NSLog(@"Second student's name is %@", ((Student *)school.students.allObjects[1]).name); // Livia
使用示例
+ (void)initialize {
[self setKeyMapping:@{@"fog": @"fog"}];
[self setValueTransformers:@{@"fog": ^id(id value) { return @(((NSString *)value).boolValue); }}];
}
Weather *weather = [Weather createFromDictionary:@{@"fog": @"YES"}];
NSLog(@"Fog = %@", weather.fog); // 1
从持久化存储协调器中的第一个持久存储中删除所有数据
[MAGCoreData deleteAll];
删除具有默认名称的存储
[MAGCoreData deleteAllInStorageWithName:nil];
删除具有特定名称的存储
[MAGCoreData deleteAllInStorageWithName:storageName];
您可以定义预处理宏 MAGCOREDATA_LOGGING_ENABLED
启用 MAGCoreData 日志记录。
我们建议您使用 mongodb生成器。 MongoDB生成器从 Core Data 模型 (.xcdatamodel) 生成模型类,并为您的类添加辅助函数以简化它们的用法。
MongoDB生成器脚本示例
mogenerator -m "${PROJECT_DIR}/MAGCoreDataExample/Models/Model.xcdatamodeld/Model.xcdatamodel" -M "${PROJECT_DIR}/MAGCoreDataExample/Models/MachineModel" -H "${PROJECT_DIR}/MAGCoreDataExample/Models/HumanModel" --template-var arc=true
MAGCoreData 由 MadAppGang 拥有和维护。
MAGCoreData 根据 MIT 许可发布。有关详细信息,请参阅 LICENSE。