TestsTested | ✗ |
LangLanguage | Obj-CObjective C |
License | MIT |
ReleasedLast Release | Aug 2017 |
Maintained by Enix Yu.
An implementation of Core Data Helper origin from Textbook: 《Learning Core Data for iOS》.
pod 'CKCoreDataHelper'
// 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
There are two importer you can use to import data into persistent store. One for XML format, another for JSON format.
// 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);
}];
// 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);
}];
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 *predicate = [NSPredicate predicateWithFormat:@"SELF.name == %@", @"Enix"];
[fetchRequest setPredicate:predicate];
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];