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那樣,利用.的形式來賦值取值呢?答案是確定的.