Core Data是iOS中十分重要的一种数据持久化的方式,以前存储数据一直使用的FMDB,不多使用Core Data;可是,仍是有必要来了解一下这个高大上的数据持久化的方式的.
因为笔者对此认知有限,不正确的地方还请指正,感谢!sql
开始以前,咱们先来了解一下这几个对象:app
NSManagedObjectModel
这个是应用程序的数据模型,这个模型包含实体(Entity),属性(Property),读取请求(Fetch Request)等oop
NSManagedObjectContext
参与对数据对象进行各类操做的全过程,并检测数据对象的变化,以提供对undo/redo的支持及更新绑定到数据的UI
NSManagedObject
数据记录
NSEntityDescription
数据实体对象
NSPersistentStoreCoordinator
主要处理底层对数据的读取与写入操做,通常咱们不须要与他接触;
另外:.xcdatamodeld文件编译后为.momd或者.mom文件测试
想要使用Core Data,须要在新建工程的时候勾选(固然也能够手动建立),即在输入工程名称的界面勾选Use Core Data ,以下图:fetch
这样新建的工程,Xcode会帮咱们添加一些与Core Data相关的内容,你会发现,在项目的左边列表,多了一个以xcdatamodeld为后缀名的文件,这就是咱们的Data Model文件:this
这个是默认生成的,固然咱们也能够手动添加,和新建其余文件同样:右键-->New File...选择左侧Core Data --> Data Modelatom
Next,输入名称,Create 便可!
和未勾选Core Data的工程的区别还有,就是在AppDelegate中,其中.h文件中多了如下内容:spa
#import <UIKit/UIKit.h> #import <CoreData/CoreData.h> @interface AppDelegate : UIResponder <UIApplicationDelegate> @property (strong, nonatomic) UIWindow *window; @property (readonly, strong, nonatomic) NSManagedObjectContext *managedObjectContext; @property (readonly, strong, nonatomic) NSManagedObjectModel *managedObjectModel; @property (readonly, strong, nonatomic) NSPersistentStoreCoordinator *persistentStoreCoordinator; - (void)saveContext; - (NSURL *)applicationDocumentsDirectory; @end
在.m文件中,增长了相关的方法的实现,通常在文件的最下面能够看到!代理
- (void)applicationWillTerminate:(UIApplication *)application { // Called when the application is about to terminate. Save data if appropriate. See also applicationDidEnterBackground:. // Saves changes in the application's managed object context before the application terminates. [self saveContext]; } #pragma mark - Core Data stack @synthesize managedObjectContext = _managedObjectContext; @synthesize managedObjectModel = _managedObjectModel; @synthesize persistentStoreCoordinator = _persistentStoreCoordinator; - (NSURL *)applicationDocumentsDirectory { // The directory the application uses to store the Core Data store file. This code uses a directory named "com.artup.LZCoreData" in the application's documents directory. return [[[NSFileManager defaultManager] URLsForDirectory:NSDocumentDirectory inDomains:NSUserDomainMask] lastObject]; } - (NSManagedObjectModel *)managedObjectModel { // The managed object model for the application. It is a fatal error for the application not to be able to find and load its model. if (_managedObjectModel != nil) { return _managedObjectModel; } NSURL *modelURL = [[NSBundle mainBundle] URLForResource:@"LZCoreData" withExtension:@"momd"]; _managedObjectModel = [[NSManagedObjectModel alloc] initWithContentsOfURL:modelURL]; return _managedObjectModel; } - (NSPersistentStoreCoordinator *)persistentStoreCoordinator { // The persistent store coordinator for the application. This implementation creates and returns a coordinator, having added the store for the application to it. if (_persistentStoreCoordinator != nil) { return _persistentStoreCoordinator; } // Create the coordinator and store _persistentStoreCoordinator = [[NSPersistentStoreCoordinator alloc] initWithManagedObjectModel:[self managedObjectModel]]; NSURL *storeURL = [[self applicationDocumentsDirectory] URLByAppendingPathComponent:@"LZCoreData.sqlite"]; NSError *error = nil; NSString *failureReason = @"There was an error creating or loading the application's saved data."; if (![_persistentStoreCoordinator addPersistentStoreWithType:NSSQLiteStoreType configuration:nil URL:storeURL options:nil error:&error]) { // Report any error we got. NSMutableDictionary *dict = [NSMutableDictionary dictionary]; dict[NSLocalizedDescriptionKey] = @"Failed to initialize the application's saved data"; dict[NSLocalizedFailureReasonErrorKey] = failureReason; dict[NSUnderlyingErrorKey] = error; error = [NSError errorWithDomain:@"YOUR_ERROR_DOMAIN" code:9999 userInfo:dict]; // Replace this with code to handle the error appropriately. // abort() causes the application to generate a crash log and terminate. You should not use this function in a shipping application, although it may be useful during development. NSLog(@"Unresolved error %@, %@", error, [error userInfo]); abort(); } return _persistentStoreCoordinator; } - (NSManagedObjectContext *)managedObjectContext { // Returns the managed object context for the application (which is already bound to the persistent store coordinator for the application.) if (_managedObjectContext != nil) { return _managedObjectContext; } NSPersistentStoreCoordinator *coordinator = [self persistentStoreCoordinator]; if (!coordinator) { return nil; } _managedObjectContext = [[NSManagedObjectContext alloc] initWithConcurrencyType:NSMainQueueConcurrencyType]; [_managedObjectContext setPersistentStoreCoordinator:coordinator]; return _managedObjectContext; } #pragma mark - Core Data Saving support - (void)saveContext { NSManagedObjectContext *managedObjectContext = self.managedObjectContext; if (managedObjectContext != nil) { NSError *error = nil; if ([managedObjectContext hasChanges] && ![managedObjectContext save:&error]) { // Replace this implementation with code to handle the error appropriately. // abort() causes the application to generate a crash log and terminate. You should not use this function in a shipping application, although it may be useful during development. NSLog(@"Unresolved error %@, %@", error, [error userInfo]); abort(); } } }
选择LZCoreData.xcdatamodeld文件,全部的可视化操做都是在这个文件进行的;code
点击下方的+按钮(Add Entity),输入你要创建的实体名称,这里是PeopleEntity
属性主要显示在中间的Attributes一栏,点击其左下的+,能够新加一个属性,输入名称,并选择一个类型,这里定义一个String类型的name属性:
注意:这里的属性名称首字母必须小写,不然会有以下提示:
这里的数据类型中,有一个比较特殊:Transformble,对应于OC中的id类型;
一样方式,再添加age和sex属性;
而后,再添加一个实体ManEntity,属性设置以下:
这里已创建PeopleEntity和ManEntity之间的联系为例,
选择PeopleEntity实体,在中间的Relationships一栏,点击+,添加一个联系,起一个名称,并在Destination中选择ManEntity:
这里的做用至关于,在实体PeopleEntity中添加了一个类型为ManEntity,名称为manRelationship的属性;
Apple官方建议咱们在创建一个目标关系后,要创建一个返回关系;即在ManEntity中创建一个和PeopleEntity的联系;
选中一个联系后,在右侧能够设置联系的一些属性:
例如Delete Rule(删除规则),Type(联系类型,一对多,一对一)等;
这样,咱们的数据模型就创建好了,接下来的事情,就是如何去使用了.
接下来的操做主要就是写代码了,来到咱们的ViewController.m文件,导入AppDelegate.h头文件,咱们须要用到里面关于Core Data的一些内容;新建一个方法test1,添加如下代码:
//获取代理 AppDelegate *delegate = [[UIApplication sharedApplication] delegate]; //获取context NSManagedObjectContext *context = [delegate managedObjectContext]; //获取PeopleEntity实体 NSManagedObject *people = [NSEntityDescription insertNewObjectForEntityForName:@"PeopleEntity" inManagedObjectContext:context]; //设置属性内容 [people setValue:@"流火绯瞳" forKey:@"name"]; [people setValue:@26 forKey:@"age"]; [people setValue:@0 forKey:@"sex"]; //获取ManEntit实体 NSManagedObject *man = [NSEntityDescription insertNewObjectForEntityForName:@"ManEntity" inManagedObjectContext:context]; [man setValue:@178.0 forKey:@"height"]; [man setValue:@60.0 forKey:@"weight"]; [man setValue:@"张三" forKey:@"name"]; [man setValue:people forKey:@"peopleRelationship"]; [people setValue:man forKey:@"manRelationship"]; NSError *error; //保存更改 if ([context save:&error]) { NSLog(@"保存成功"); } else { NSLog(@"Whoops, couldn't save: %@", [error localizedDescription]); } //查询实体 //建立一个查询请求 NSFetchRequest *fetchRequest = [[NSFetchRequest alloc] init]; //获取要查询的实体 NSEntityDescription *entity = [NSEntityDescription entityForName:@"PeopleEntity" inManagedObjectContext:context]; //添加到查询请求 [fetchRequest setEntity:entity]; //开始查询并获取结果 NSArray *fetchedObjects = [context executeFetchRequest:fetchRequest error:&error]; NSLog(@"输出查询结果"); for (NSManagedObject *info in fetchedObjects) { NSLog(@"Name: %@", [info valueForKey:@"name"]); NSLog(@"age: %@", [info valueForKey:@"age"]); NSLog(@"sex: %@", [info valueForKey:@"sex"]); NSLog(@"-----------------------------------"); NSManagedObject *man1 = [info valueForKey:@"manRelationship"]; NSLog(@"Name: %@", [man1 valueForKey:@"name"]); NSLog(@"weight: %@", [man1 valueForKey:@"weight"]); NSLog(@"height: %@", [man1 valueForKey:@"height"]); NSLog(@"=========================================="); }
代码中我简单的加入了注释,这里只是为了测试一下建立的数据模型是否可用;
因此,只是使用了添加数据和查询数据的方法,而后运行程序,能够在控制台看到以下输出:
2016-05-26 15:37:25.496 LZCoreData[5654:567413] 保存成功 2016-05-26 15:37:25.497 LZCoreData[5654:567413] 输出查询结果 2016-05-26 15:37:25.498 LZCoreData[5654:567413] Name: 流火绯瞳 2016-05-26 15:37:25.498 LZCoreData[5654:567413] age: 26 2016-05-26 15:37:25.498 LZCoreData[5654:567413] sex: 0 2016-05-26 15:37:25.498 LZCoreData[5654:567413] ----------------------------------- 2016-05-26 15:37:25.498 LZCoreData[5654:567413] Name: 张三 2016-05-26 15:37:25.499 LZCoreData[5654:567413] weight: 60 2016-05-26 15:37:25.499 LZCoreData[5654:567413] height: 178 2016-05-26 15:37:25.501 LZCoreData[5654:567413] ==========================================
表示咱们成功的保存了数据,并查询了出来.你可能会发现,这里我在设置属性值和取值的时候,都是使用的KVC的模式,这样在使用时是至关不方便的,能不能像其余的Model那样,利用.的形式来赋值取值呢?答案是确定的.