CKCoreDataHelper 0.0.11

CKCoreDataHelper 0.0.11

测试已测试
语言语言 Obj-CObjective C
许可 MIT
发布最新版本2017年8月

Enix Yu 维护。



  • Enix Yu

《Learning Core Data for iOS》教材中 origin 的 Core Data Helper 实现。

安装

Pod

pod 'CKCoreDataHelper'

用法

Core Data 设置

// Your database file name
NSString *storeFileName = @"test.sqlite";

// Initialized a core data manager
CKCoreDataManager *manager = [CKCoreDataManager sharedInstanced];

// Setup core data manager with given store file
NSError *error;
BOOL flag = [manager setupCoreDataWithStoreFileName:storeFileName // Store file name
                               lightWeightMigration:YES           // enable lightweight data migration
                                              error:&error];      // get the error if init failed

导入数据

有两个导入器可以用来将数据导入持久存储。一个是 XML 格式的,另一个是 JSON 格式的。

XML

// Initialize
_importer = [[CKCoreDataXMLImporter alloc]
                 initWithCoordinator:self.manager.coordinator
                 entitiesUniqueAttributes:@{@"Product": @"name",
                                            @"Shop": @"name"}];

NSURL *productXML = [bundle URLForResource:@"preload_product" withExtension:@"xml"];
    
[_importer importDataFrom:shopXML                                         // XML URL Path
                forEntity:@"Shop"                                         // The name for the Entity you need to import to
               completion:^(BOOL success, NSError * _Nullable error) {    // A callback when import finished.
                   XCTAssertTrue(success);
                   XCTAssertNil(error);
               }];

JSON

// Initialize
_importer = [[CKCoreDataJSONImporter alloc]
                 initWithCoordinator:self.manager.coordinator
                 entitiesUniqueAttributes:@{@"Product": @"name",
                                            @"Shop": @"name"}];

NSURL *path = [bundle URLForResource:@"preload_product_withoutrel" withExtension:@"json"];

[_importer importDataFrom:path
                forEntity:@"Product"
               completion:^(BOOL success, NSError * _Nullable error) {
                   XCTAssertTrue(success);
                   XCTAssertNil(error);
               }];

实体 CURD

检索记录

NSFetchRequest *fetchRequest = [NSFetchRequest fetchRequestWithEntityName:@"Entity"];
NSArray *results = [_manager.context executeFetchRequest:fetchRequest error:nil];

排序

NSSortDescriptor *sort = [NSSortDescriptor sortDescriptorWithKey:@"attribute Name" ascending:YES];
[fetchRequest setSortDescriptors:@[sort]];

过滤(使用 NSPredicate)。 NSPredicate 手册

NSPredicate *predicate = [NSPredicate predicateWithFormat:@"SELF.name == %@", @"Enix"];
[fetchRequest setPredicate:predicate];

过滤(获取请求模板)

  1. 选择 Model.xcdatamodeld
  2. 编辑 > 添加获取请求
  3. 为您的获取请求设置名称,例如,fetchByName
  4. 添加过滤表达式
  5. 在代码中获取获取请求模板
NSFetchRequest *req = [[_manager model]
                       fetchRequestTemplateForName:@"your fetch request name"];

创建实体实例

Entity *newItem = [NSEntityDescription insertNewObjectForEntityForName:@"Entity"
                                                inManagedObjectContext:context];

删除记录

// Get the record which is pending to remove
NSArray *results = [[coreDataHelper context] executeFetchRequest:fetchRequest error:nil];

// Delete the record
[[_manager context] deleteObject:[results firstObject]];

保存

NSError *error = nil;
[context save:&error];