IOS-Core Data的使用
二、Core Data的使用(一)
?
CoreData1.??常用類和方法???(?注意需要導(dǎo)入???coredata??框架???)
表結(jié)構(gòu):NSEntityDescription
表記錄:NSManagedObject 數(shù)據(jù)庫(kù)存放方式:NSPersistentStoreCoordinator(持久化存儲(chǔ)協(xié)調(diào)者) 數(shù)據(jù)庫(kù)操作:NSManagedObjectContext(被管理的對(duì)象上下文)
一、概念
1.Core Data 是數(shù)據(jù)持久化存儲(chǔ)的最佳方式
2.數(shù)據(jù)最終的存儲(chǔ)類型可以是:SQLite數(shù)據(jù)庫(kù),XML,二進(jìn)制,內(nèi)存里,或自定義數(shù)據(jù)類型
在Mac OS X 10.5Leopard及以后的版本中,開發(fā)者也可以通過(guò)繼承NSPersistentStore類以創(chuàng)建自定義的存儲(chǔ)格式
3.好處:能夠合理管理內(nèi)存,避免使用sql的麻煩,高效
4.構(gòu)成:
(1)NSManagedObjectContext(被管理的數(shù)據(jù)上下文)
操作實(shí)際內(nèi)容(操作持久層)
作用:插入數(shù)據(jù),查詢數(shù)據(jù),刪除數(shù)據(jù)
(2)NSManagedObjectModel(被管理的數(shù)據(jù)模型)
數(shù)據(jù)庫(kù)所有表格或數(shù)據(jù)結(jié)構(gòu),包含各實(shí)體的定義信息
作用:添加實(shí)體的屬性,建立屬性之間的關(guān)系
操作方法:視圖編輯器,或代碼
(3)NSPersistentStoreCoordinator(持久化存儲(chǔ)助理)
相當(dāng)于數(shù)據(jù)庫(kù)的連接器
作用:設(shè)置數(shù)據(jù)存儲(chǔ)的名字,位置,存儲(chǔ)方式,和存儲(chǔ)時(shí)機(jī)
(4)NSManagedObject(被管理的數(shù)據(jù)記錄)
相當(dāng)于數(shù)據(jù)庫(kù)中的表格記錄
(5)NSFetchRequest(獲取數(shù)據(jù)的請(qǐng)求)
相當(dāng)于查詢語(yǔ)句
(6)NSEntityDescription(實(shí)體結(jié)構(gòu))
相當(dāng)于表格結(jié)構(gòu)
(7)后綴為.xcdatamodeld的包
里面是.xcdatamodel文件,用數(shù)據(jù)模型編輯器編輯
編譯后為.momd或.mom文件
5.依賴關(guān)系
?
?
二、基于SQLite數(shù)據(jù)庫(kù)時(shí),Core Data的簡(jiǎn)單使用
和SQLite的區(qū)別:只能取出整個(gè)實(shí)體記錄,然后分解,之后才能得到實(shí)體的某個(gè)屬性
1.構(gòu)建流程
包括:創(chuàng)建數(shù)據(jù)上下文,創(chuàng)建數(shù)據(jù)模型,創(chuàng)建數(shù)據(jù)持久化存儲(chǔ)助理
(1)若是新建的工程,選擇空白應(yīng)用程序,next
勾選Use Core Data選項(xiàng)
此時(shí)生成的工程文件AppDelegate中,會(huì)自動(dòng)生成被管理的數(shù)據(jù)上下文等相關(guān)代碼
(2)比如AppDelegate.h文件中,自動(dòng)生成
@property (readonly, strong, nonatomic) NSManagedObjectContext *managedObjectContext; @property (readonly, strong, nonatomic) NSManagedObjectModel *managedObjectModel; @property (readonly, strong, nonatomic) NSPersistentStoreCoordinator *persistentStoreCoordinator;- (void)saveContext; - (NSURL *)applicationDocumentsDirectory;方法saveContext表示:保存數(shù)據(jù)到持久層(數(shù)據(jù)庫(kù))
方法applicationDocumentsDirectory表示:應(yīng)用程序沙箱下的Documents目錄路徑
(例如/var/mobile/Applications/5FG80A45-DFB5-4087-A1B1-41342A977E21/Documents/)
(3)比如AppDelegate.h文件中,自動(dòng)生成
@synthesize managedObjectContext = __managedObjectContext; @synthesize managedObjectModel = __managedObjectModel; @synthesize persistentStoreCoordinator = __persistentStoreCoordinator;保存數(shù)據(jù)到持久層
- (void)applicationWillTerminate:(UIApplication *)application {[self saveContext]; } - (void)saveContext {NSError *error = nil;NSManagedObjectContext *managedObjectContext = self.managedObjectContext;if (managedObjectContext != nil) {if ([managedObjectContext hasChanges] && ![managedObjectContext save:&error]) {NSLog(@"Unresolved error %@, %@", error, [error userInfo]);abort();} } }Documents目錄路徑
- (NSURL *)applicationDocumentsDirectory {return [[[NSFileManager defaultManager] URLsForDirectory:NSDocumentDirectory inDomains:NSUserDomainMask] lastObject]; }被管理的數(shù)據(jù)上下文
初始化的后,必須設(shè)置持久化存儲(chǔ)助理
- (NSManagedObjectContext *)managedObjectContext {if (__managedObjectContext != nil) {return __managedObjectContext;}NSPersistentStoreCoordinator *coordinator = [self persistentStoreCoordinator];if (coordinator != nil) {__managedObjectContext = [[NSManagedObjectContext alloc] init];[__managedObjectContext setPersistentStoreCoordinator:coordinator];}return __managedObjectContext; }?
被管理的數(shù)據(jù)模型
初始化必須依賴.momd文件路徑,而.momd文件由.xcdatamodeld文件編譯而來(lái)
- (NSManagedObjectModel *)managedObjectModel {if (__managedObjectModel != nil) {return __managedObjectModel;}NSURL *modelURL = [[NSBundle mainBundle] URLForResource:@"TestApp" withExtension:@"momd"];__managedObjectModel = [[NSManagedObjectModel alloc] initWithContentsOfURL:modelURL];return __managedObjectModel; }持久化存儲(chǔ)助理
初始化必須依賴NSManagedObjectModel,之后要指定持久化存儲(chǔ)的數(shù)據(jù)類型,默認(rèn)的是NSSQLiteStoreType,即SQLite數(shù)據(jù)庫(kù);并指定存儲(chǔ)路徑為Documents目錄下,以及數(shù)據(jù)庫(kù)名稱
- (NSPersistentStoreCoordinator *)persistentStoreCoordinator {if (__persistentStoreCoordinator != nil) {return __persistentStoreCoordinator;}NSURL *storeURL = [[self applicationDocumentsDirectory] URLByAppendingPathComponent:@"TestApp.sqlite"];NSError *error = nil;__persistentStoreCoordinator = [[NSPersistentStoreCoordinator alloc] initWithManagedObjectModel:[self managedObjectModel]];
if (![__persistentStoreCoordinator addPersistentStoreWithType:NSSQLiteStoreType configuration:nil URL:storeURL options:nil error:&error]) {NSLog(@"Unresolved error %@, %@", error, [error userInfo]);abort();} return __persistentStoreCoordinator; }
如果不是新工程,也可以自己寫入相關(guān)代碼
(4)此外還生成了TestApp.xcdatamodeld文件
(5)還自動(dòng)鏈接了CoreData.framework
(6)在預(yù)編譯頭.pch文件中,加入導(dǎo)入了CoreData.h頭文件
#import <CoreData/CoreData.h>?
2.創(chuàng)建數(shù)據(jù)模型(數(shù)據(jù)模型編輯器操作)
(1)創(chuàng)建實(shí)體
選中.xcodedatamodel對(duì)象
在右邊的數(shù)據(jù)模型編輯器的底部工具欄點(diǎn)擊Add Entity添加實(shí)體
在最右側(cè)欄對(duì)實(shí)體命名
(2)創(chuàng)建實(shí)體屬性
選中實(shí)體,點(diǎn)擊底部工具欄的Add Attribute添加屬性
選中新添加的屬性,對(duì)屬性進(jìn)行命名,并設(shè)置屬性的數(shù)據(jù)類型Attribute Type
(3)為兩個(gè)實(shí)體建立關(guān)系
選中一個(gè)實(shí)體,在底部工具欄點(diǎn)擊Add Relationship添加關(guān)系
選中新關(guān)系,對(duì)關(guān)系添加名稱,目標(biāo)destination設(shè)置為另個(gè)實(shí)體
(4)建立返回關(guān)系
(當(dāng)你建立一個(gè)目標(biāo)關(guān)系,最好建立一個(gè)返回關(guān)系)
在另一個(gè)實(shí)體中建立一個(gè)關(guān)系并命名,設(shè)置目標(biāo)對(duì)象為這之前的實(shí)體
并在Inverse屬性選這之前的關(guān)系名稱
?
(5)設(shè)置兩個(gè)關(guān)系的刪除規(guī)則Delete Rule,都為關(guān)聯(lián)模式
關(guān)聯(lián)模式cascade:其中一個(gè)數(shù)據(jù)被刪除,另一個(gè)實(shí)體中的數(shù)據(jù)也會(huì)跟著刪除
(6)最終兩個(gè)對(duì)象的關(guān)系圖為
切換Editor Stype按鈕
會(huì)看到另一種編輯方式:
?3.插入數(shù)據(jù)
在AppDelegate.m的application:didFinishLaunchingWithOptions:方法里,調(diào)用自定義方法
insertCoreData插入數(shù)據(jù),代碼如下:
- (void)insertCoreData {NSManagedObjectContext *context = [self managedObjectContext];NSManagedObject *contactInfo = [NSEntityDescription insertNewObjectForEntityForName:@"ContactInfo" inManagedObjectContext:context];[contactInfo setValue:@"name B" forKey:@"name"];[contactInfo setValue:@"birthday B" forKey:@"birthday"];[contactInfo setValue:@"age B" forKey:@"age"];NSManagedObject *contactDetailInfo = [NSEntityDescription insertNewObjectForEntityForName:@"ContactDetailInfo" inManagedObjectContext:context];[contactDetailInfo setValue:@"address B" forKey:@"address"];[contactDetailInfo setValue:@"name B" forKey:@"name"];[contactDetailInfo setValue:@"telephone B" forKey:@"telephone"];[contactDetailInfo setValue:contactInfo forKey:@"info"];[contactInfo setValue:contactDetailInfo forKey:@"details"];NSError *error;if(![context save:&error]){NSLog(@"不能保存:%@",[error localizedDescription]);} }創(chuàng)建數(shù)據(jù)上下文,調(diào)用insertNewObjectForName方法,創(chuàng)建兩個(gè)數(shù)據(jù)記錄NSManagedObject,然后就可以對(duì)之前數(shù)據(jù)模型編輯視圖中定義的屬性進(jìn)行賦值。此時(shí)的數(shù)據(jù)只在內(nèi)存中被修改,最后調(diào)用數(shù)據(jù)上下文的save方法,保存到持久層
4.查詢數(shù)據(jù)
在調(diào)用了insertCoreData之后,可以調(diào)用自定的查詢方法dataFetchRequest來(lái)查詢插入的數(shù)據(jù)
- (void)dataFetchRequest {NSManagedObjectContext *context = [self managedObjectContext];NSFetchRequest *fetchRequest = [[[NSFetchRequest alloc] init] autorelease];NSEntityDescription *entity = [NSEntityDescription entityForName:@"ContactInfo" inManagedObjectContext:context];[fetchRequest setEntity:entity];NSError *error;NSArray *fetchedObjects = [context executeFetchRequest:fetchRequest error:&error];for (NSManagedObject *info in fetchedObjects) {NSLog(@"name:%@", [info valueForKey:@"name"]);NSLog(@"age:%@", [info valueForKey:@"age"]);NSLog(@"birthday:%@", [info valueForKey:@"birthday"]);NSManagedObject *details = [info valueForKey:@"details"];NSLog(@"address:%@", [details valueForKey:@"address"]);NSLog(@"telephone:%@", [details valueForKey:@"telephone"]);} }fetchRequest相當(dāng)于sql查詢語(yǔ)句的包裝類,需要用setEntity方法,來(lái)指定具體查詢的實(shí)體結(jié)構(gòu)(表結(jié)構(gòu))
通過(guò)NSEntityDescription的entityForName方法來(lái),返回指向該具體實(shí)體結(jié)構(gòu)的指針
然后調(diào)用executeFetchRequest:error:方法,來(lái)執(zhí)行查詢操作,如果操作成功,則返回對(duì)應(yīng)的數(shù)據(jù)記錄數(shù)組
其中,可以通過(guò)NSManagedObject數(shù)據(jù)記錄對(duì)象里關(guān)聯(lián)的屬性,查詢另一個(gè)數(shù)據(jù)記錄對(duì)象里的屬性
5.數(shù)據(jù)模版
為每個(gè)實(shí)體生成一個(gè)NSManagedObject子類
上面設(shè)置數(shù)據(jù)和獲取數(shù)據(jù)時(shí),使用的是Key-Value方式,更好的方法是通過(guò)生成強(qiáng)類型的NSManagedObject的子類,通過(guò)類的成員屬性來(lái)訪問(wèn)和獲取數(shù)據(jù)
(1)在數(shù)據(jù)編輯器視圖中選中實(shí)體對(duì)象,
選則file菜單,點(diǎn)擊new,點(diǎn)擊file...,選擇Core Data項(xiàng),選擇NSManagedObject subclass,生成該實(shí)體同名的類,
繼承于NSManagedObject
生成對(duì)應(yīng)的ContactInfo.h文件
#import <Foundation/Foundation.h> #import <CoreData/CoreData.h>@class ContactDetailInfo;@interface ContactInfo : NSManagedObject@property (nonatomic, retain) NSString * age; @property (nonatomic, retain) NSString * birthday; @property (nonatomic, retain) NSString * name; @property (nonatomic, retain) ContactDetailInfo *details;@end和ContactInfo.m文件
其中,@dynamic告訴編譯器不做處理,使編譯通過(guò),其getter和setter方法會(huì)在運(yùn)行時(shí)動(dòng)態(tài)創(chuàng)建,由Core Data框架為此類屬性生成存取方法
#import "ContactInfo.h" #import "ContactDetailInfo.h"@implementation ContactInfo@dynamic age; @dynamic birthday; @dynamic name; @dynamic details;@end以及ContactDetailInfo.h文件
#import <Foundation/Foundation.h> #import <CoreData/CoreData.h>@class ContactInfo;@interface ContactDetailInfo : NSManagedObject@property (nonatomic, retain) NSString * address; @property (nonatomic, retain) NSString * name; @property (nonatomic, retain) NSString * telephone; @property (nonatomic, retain) ContactInfo *info;@end和ContactDetailInfo.m文件
#import "ContactDetailInfo.h" #import "ContactInfo.h"@implementation ContactDetailInfo@dynamic address; @dynamic name; @dynamic telephone; @dynamic info;@end此時(shí),數(shù)據(jù)模型編輯器視圖最右邊欄中,實(shí)體的class就變成具體的類名
之前用Key-Value的代碼就可以修改為:
#import "ContactInfo.h" #import "ContactDetailInfo.h" - (void)insertCoreData {NSManagedObjectContext *context = [self managedObjectContext];ContactInfo *contactInfo = [NSEntityDescription insertNewObjectForEntityForName:@"ContactInfo" inManagedObjectContext:context];contactInfo.name = @"name B";contactInfo.birthday = @"birthday B";contactInfo.age = @"age B";ContactDetailInfo *contactDetailInfo = [NSEntityDescription insertNewObjectForEntityForName:@"ContactDetailInfo" inManagedObjectContext:context];contactDetailInfo.address = @"address B";contactDetailInfo.name = @"name B";contactDetailInfo.telephone = @"telephone B";contactDetailInfo.info = contactInfo;contactInfo.details = contactDetailInfo;NSError *error;if(![context save:&error]){NSLog(@"不能保存:%@",[error localizedDescription]);} } - (void)dataFetchRequest {NSManagedObjectContext *context = [self managedObjectContext];NSFetchRequest *fetchRequest = [[[NSFetchRequest alloc] init] autorelease];NSEntityDescription *entity = [NSEntityDescription entityForName:@"ContactInfo" inManagedObjectContext:context];[fetchRequest setEntity:entity];NSError *error;NSArray *fetchedObjects = [context executeFetchRequest:fetchRequest error:&error];for (ContactInfo *info in fetchedObjects) {NSLog(@"name:%@", info.name);NSLog(@"age:%@", info.age);NSLog(@"birthday:%@", info.birthday);ContactDetailInfo *details = info.details;NSLog(@"address:%@", details.address);NSLog(@"telephone:%@", details.telephone);} }?
三、數(shù)據(jù)庫(kù)相關(guān)
1.打印隱藏的sql語(yǔ)句:
在Edit Scheme中選擇Run,之后進(jìn)入Arguments標(biāo)簽,添加參數(shù):“-com.apple.CoreData.SQLDebug 1”
2.使用SQLite存儲(chǔ)時(shí),數(shù)據(jù)庫(kù)結(jié)構(gòu)
存儲(chǔ)的SQLite數(shù)據(jù)庫(kù)表名稱:大寫“Z”加上實(shí)體名稱大寫,一個(gè)實(shí)體相當(dāng)于一張表
具體的字段名稱:大寫“Z”加上實(shí)體屬性名稱大寫
?
?
?
二、Core Data的使用(二)
?
一、基礎(chǔ)概念深入
1.NSManagedObjectContext
被管理數(shù)據(jù)上下文就像便箋簿
當(dāng)從數(shù)據(jù)持久層獲取數(shù)據(jù)時(shí),相當(dāng)于把這些臨時(shí)的數(shù)據(jù)拷貝寫在便箋簿上,然后就可以隨心所欲的修改這些值。
通過(guò)上下文,可以對(duì)數(shù)據(jù)記錄NSManagedObject進(jìn)行添加刪除更改,記錄更改后支持撤銷和重做。
除非你保存這些數(shù)據(jù)變化,否則持久層的東西是不會(huì)變化。
通常我們將 controller 類或其子類與 Managed Object Context NSManagedObjectContext綁定,這樣就方便我們動(dòng)態(tài)地生成,獲取數(shù)據(jù)對(duì)象等。
?常用的方法:
?
| -save: | 將數(shù)據(jù)對(duì)象保存到數(shù)據(jù)文件 |
| -objectWithID: | 查詢指定 Managed Object ID 的數(shù)據(jù)對(duì)象 |
| -deleteObject: | 將一個(gè)數(shù)據(jù)對(duì)象標(biāo)記為刪除,但是要等到 Context 提交更改時(shí)才真正刪除數(shù)據(jù)對(duì)象 |
| -undo | 回滾最后一步操作,這是都 undo/redo 的支持 |
| -lock | 加鎖,常用于多線程以及創(chuàng)建事務(wù)。同類接口還有:-unlock and -tryLock |
| -rollback | 還原數(shù)據(jù)文件內(nèi)容 |
| -reset | 清除緩存的 Managed Objects。只應(yīng)當(dāng)在添加或刪除 Persistent Stores 時(shí)使用 |
| -undoManager | 返回當(dāng)前 Context 所使用的 NSUndoManager |
| -assignObject: toPersistantStore: | 由于 Context 可以管理從不同數(shù)據(jù)文件而來(lái)的數(shù)據(jù)對(duì)象, 這個(gè)接口的作用就是指定數(shù)據(jù)對(duì)象的存儲(chǔ)數(shù)據(jù)文件(通過(guò)指定 PersistantStore 實(shí)現(xiàn)) |
| -executeFetchRequest: error: | 執(zhí)行獲取數(shù)據(jù)請(qǐng)求,返回所有匹配的數(shù)據(jù)對(duì)象 |
?
2.NSManagedObject
被管理的數(shù)據(jù)記錄,相當(dāng)于數(shù)據(jù)庫(kù)中的一條記錄
每一個(gè)NSManagedObject對(duì)象,都有一個(gè)全局 ID(類型為:NSManagedObjectID)。每個(gè)在NSManagedObjectContext注冊(cè)過(guò)
的NSManagedObject,可以通過(guò)這個(gè)全局 ID 在上下文中查詢到。
每個(gè)在持久存儲(chǔ)層中的對(duì)象,都對(duì)應(yīng)一個(gè)與上下文相關(guān)的NSManagedObject
常用的方法:
-entity 獲取實(shí)體
-objectID 獲取NSManagedObjectID
-valueForKey:?獲取指定 Property 的值
-setValue: forKey:?設(shè)定指定 Property 的值
3.NSFetchRequest
獲取數(shù)據(jù)的請(qǐng)求,通過(guò)被管理數(shù)據(jù)的上下文來(lái)執(zhí)行查詢,比如
NSArray *fetchedObjects = [context executeFetchRequest:fetchRequest error:&error];查詢時(shí),必須指定查詢實(shí)體或?qū)嶓w名稱,以 NSArray 形式返回查詢結(jié)果,如果我們沒(méi)有設(shè)置任何查詢條件,則返回該 Entity 的所有數(shù)據(jù)對(duì)象。
我們可以使用謂詞來(lái)設(shè)置查詢條件,通常會(huì)將常用的 Fetch Requests 保存到 dictionary 以重復(fù)利用。
NSFetchRequest包括以下部分:
(1)實(shí)體(Entity)的名稱
(2)NSPredicate謂詞(搜索關(guān)鍵字或限定條件)
(3)排序方式(NSArray?*)sortDescriptors
所有的被管理對(duì)象(managed object)都必須在上下文中注冊(cè),而通過(guò)NSFetchRequest獲得的對(duì)象自動(dòng)被注冊(cè)。
如果在上下文中已經(jīng)存在了要獲取的對(duì)象,那么這個(gè)被管理NSManagedObject將被返回。否則上下文就會(huì)從相關(guān)的數(shù)據(jù)源中查找(也可能找不到)
例如,以下代碼是查詢?cè)谥付ㄈ掌谥髣?chuàng)建的ContactInfo,并將查詢結(jié)果按照name排序
NSManagedObjectContext * context = [self managedObjectContext]; NSManagedObjectModel * model = [self managedObjectModel]; NSDictionary * entities = [model entitiesByName]; NSEntityDescription * entity = [entities valueForKey:@"ContactInfo"];NSPredicate * predicate; predicate = [NSPredicate predicateWithFormat:@"creationDate > %@", date];NSSortDescriptor * sort = [[NSortDescriptor alloc] initWithKey:@"name"]; NSArray * sortDescriptors = [NSArray arrayWithObject: sort];NSFetchRequest * fetch = [[NSFetchRequest alloc] init]; [fetch setEntity: entity]; [fetch setPredicate: predicate]; [fetch setSortDescriptors: sortDescriptors];NSArray * results = [context executeFetchRequest:fetch error:nil]; [sort release]; [fetch release];常用方法:
| -setEntity: | 設(shè)置你要查詢的數(shù)據(jù)對(duì)象的類型(Entity) |
| -setPredicate: | 設(shè)置查詢條件 |
| -setFetchLimit: | 設(shè)置最大查詢對(duì)象數(shù)目 |
| -setSortDescriptors: | 設(shè)置查詢結(jié)果的排序方法 |
| -setAffectedStores: | 設(shè)置可以在哪些數(shù)據(jù)存儲(chǔ)中查詢 |
4.NSPersistentStoreCoordinator
持久化數(shù)據(jù)助理
Core Data定義了一個(gè)棧,持久化存儲(chǔ)助理在中間,棧頂是被管理數(shù)據(jù)的上下文,棧底是持久化存儲(chǔ)層,結(jié)構(gòu)如圖
通常從磁盤上的數(shù)據(jù)文件中讀取或存儲(chǔ)數(shù)據(jù),這些底層的讀寫就由它來(lái)處理。一般我們無(wú)需與它直接打交道,上下文已經(jīng)封裝了對(duì)它的調(diào)用
常用方法:
?
-addPersistentStoreForURL:configuration:URL:options:error: | 加載持久化存儲(chǔ)數(shù)據(jù),對(duì)應(yīng)的卸載接口為 -removePersistentStore:error: |
| -migratePersistentStore:toURL:options:withType:error: | 遷移數(shù)據(jù)存儲(chǔ),效果與 "save as"相似,但是操作成功后, 遷移前的數(shù)據(jù)存儲(chǔ)不可再使用 |
| -managedObjectIDForURIRepresentation: | 返回給定 URL所指示的數(shù)據(jù)存儲(chǔ)的 object id,如果找不到匹配的數(shù)據(jù)存儲(chǔ)則返回 nil |
| -persistentStoreForURL: | 返回指定路徑的 Persistent Store |
| -URLForPersistentStore: | 返回指定 Persistent Store 的存儲(chǔ)路徑 |
5.NSManagedObjectModel
被管理的數(shù)據(jù)模型,用來(lái)描述程序的實(shí)體、其屬性、關(guān)系的模型圖
包括以下幾個(gè)部分:
(1)實(shí)體(Entity)
對(duì)應(yīng)NSEntityDescription對(duì)象
相當(dāng)于數(shù)據(jù)庫(kù)中的一個(gè)表
實(shí)體名稱(name)
實(shí)體類名:NSManagedObject子類的名稱
實(shí)體實(shí)例:NSManagedObject對(duì)象或其子類的實(shí)例
NSEntityDescription 常用方法:
+insertNewObjectForEntityForName:inManagedObjectContext: 工廠方法,
根據(jù)給定的 Entity 描述,生成相應(yīng)的 NSManagedObject 對(duì)象,并插入 ManagedObjectContext 中。
-managedObjectClassName返回映射到 Entity 的 NSManagedObject 類名
-attributesByName以名字為 key, 返回 Entity 中對(duì)應(yīng)的 Attributes
-relationshipsByName以名字為 key, 返回 Entity 中對(duì)應(yīng)的 Relationships
(2)屬性(Property)
對(duì)應(yīng)NSPropertyDescription對(duì)象
Property 為 Entity 的特性,它相當(dāng)于數(shù)據(jù)庫(kù)表中的一列,或者 XML 文件中的 value-key 對(duì)中的 key。
它可以描述實(shí)體基本屬性(Attribute),實(shí)體之間的關(guān)系(RelationShip),或查詢屬性(Fetched Property)。
<1> 實(shí)體的基本屬性(Attributes)
對(duì)應(yīng)NSAttributeDescription對(duì)象
存儲(chǔ)基本數(shù)據(jù),數(shù)據(jù)類型包括:
string,date,integer(NSString, NSDate, NSNumber)
<2> 實(shí)體間的關(guān)系(Relationships)
對(duì)應(yīng)NSRelationshipDescription對(duì)象
支持對(duì)一、對(duì)多的關(guān)系
<3>?查詢屬性(Fetched Property)
對(duì)應(yīng)NSFetchedPropertyDescription對(duì)象
根據(jù)查詢謂詞返回指定實(shí)體的符合條件的數(shù)據(jù)對(duì)象
表示了一種“弱”的、單項(xiàng)的關(guān)系(相當(dāng)于數(shù)據(jù)庫(kù)中的查詢語(yǔ)句)
?6.持久化存儲(chǔ)層(Persistent Stores)
持久化存儲(chǔ)層是和文件或外部數(shù)據(jù)庫(kù)關(guān)聯(lián)的,大多數(shù)訪問(wèn)持久化存儲(chǔ)層的動(dòng)作都由上下文來(lái)完成。
7.NSFetchedResultsController
?用于在表視圖table view中加載部分?jǐn)?shù)據(jù)
?
二、用代碼創(chuàng)建數(shù)據(jù)模型
NSManagedObjectModel *managedObjectModel() {static NSManagedObjectModel *moModel = nil;if (moModel != nil) {return moModel;}moModel = [[NSManagedObjectModel alloc] init];// Create the entity NSEntityDescription *runEntity = [[NSEntityDescription alloc] init];[runEntity setName:@"Run"];[runEntity setManagedObjectClassName:@"Run"];[moModel setEntities:[NSArray arrayWithObject:runEntity]];// Add the Attributes NSAttributeDescription *dateAttribute = [[NSAttributeDescription alloc] init];[dateAttribute setName:@"date"];[dateAttribute setAttributeType:NSDateAttributeType];[dateAttribute setOptional:NO];NSAttributeDescription *idAttribute = [[NSAttributeDescription alloc] init];[idAttribute setName:@"processID"];[idAttribute setAttributeType:NSInteger32AttributeType];[idAttribute setOptional:NO];[idAttribute setDefaultValue:[NSNumber numberWithInteger:-1]];// Create the validation predicate for the process ID.// The following code is equivalent to validationPredicate = [NSPredicate predicateWithFormat:@"SELF > 0"] NSExpression *lhs = [NSExpression expressionForEvaluatedObject];NSExpression *rhs = [NSExpression expressionForConstantValue:[NSNumber numberWithInteger:0]];NSPredicate *validationPredicate = [NSComparisonPredicatepredicateWithLeftExpression:lhsrightExpression:rhsmodifier:NSDirectPredicateModifiertype:NSGreaterThanPredicateOperatorTypeoptions:0];NSString *validationWarning = @"Process ID < 1";[idAttribute setValidationPredicates:[NSArray arrayWithObject:validationPredicate]withValidationWarnings:[NSArray arrayWithObject:validationWarning]];NSArray *properties = [NSArray arrayWithObjects: dateAttribute, idAttribute, nil];[runEntity setProperties:properties];// Add a Localization Dictionary NSMutableDictionary *localizationDictionary = [NSMutableDictionary dictionary];[localizationDictionary setObject:@"Date" forKey:@"Property/date/Entity/Run"];[localizationDictionary setObject:@"Process ID" forKey:@"Property/processID/Entity/Run"];[localizationDictionary setObject:@"Process ID must not be less than 1" forKey:@"ErrorString/Process ID < 1"];[moModel setLocalizationDictionary:localizationDictionary];return moModel; }1)我們創(chuàng)建了一個(gè)全局模型?moModel;
2)并在其中創(chuàng)建一個(gè)名為?Run 的 Entity,這個(gè) Entity 對(duì)應(yīng)的?ManagedObject 類名為?Run(很快我們將創(chuàng)建這樣一個(gè)類);
3)給 Run Entity 添加了兩個(gè)必須的 Property:date 和 processID,分別表示運(yùn)行時(shí)間以及進(jìn)程 ID;并設(shè)置默認(rèn)的進(jìn)程 ID 為 -1;
4)給 processID 特性設(shè)置檢驗(yàn)條件:必須大于 0;
5)給模型設(shè)置本地化描述詞典;
本地化描述提供對(duì) Entity,Property,Error信息等的便于理解的描述,其可用的鍵值對(duì)如下表:
| Key | Value |
| "Entity/NonLocalizedEntityName" | "LocalizedEntityName" |
| "Property/NonLocalizedPropertyName/Entity/EntityName" | "LocalizedPropertyName" |
| "Property/NonLocalizedPropertyName" | "LocalizedPropertyName" |
| "ErrorString/NonLocalizedErrorString" | "LocalizedErrorString" |
?
三、存儲(chǔ)數(shù)據(jù)到xml文件
存儲(chǔ)類型為NSXMLStoreType
NSManagedObjectContext *managedObjectContext() {static NSManagedObjectContext *moContext = nil;if (moContext != nil) {return moContext;}moContext = [[NSManagedObjectContext alloc] init];// Create a persistent store coordinator, then set the coordinator for the context. NSManagedObjectModel *moModel = managedObjectModel();NSPersistentStoreCoordinator *coordinator = [[NSPersistentStoreCoordinator alloc] initWithManagedObjectModel:moModel];[moContext setPersistentStoreCoordinator: coordinator];// Create a new persistent store of the appropriate type. NSString *STORE_TYPE = NSXMLStoreType;NSString *STORE_FILENAME = @"CoreDataTutorial.xml";NSError *error = nil;NSURL *url = [applicationDocmentDirectory() URLByAppendingPathComponent:STORE_FILENAME];NSPersistentStore *newStore = [coordinator addPersistentStoreWithType:STORE_TYPEconfiguration:nilURL:urloptions:nilerror:&error];if (newStore == nil) {NSLog(@"Store Configuration Failure\n%@", ([error localizedDescription] != nil) ? [error localizedDescription] : @"Unknown Error");}return moContext; }四、自定義NSManagedObject子類
比如,Run.h文件
#import <CoreData/NSManagedObject.h>@interface Run : NSManagedObject {NSInteger processID; }@property (retain) NSDate *date; @property (retain) NSDate *primitiveDate; @property NSInteger processID;@endRun.m文件
#import "Run.h"@implementation Run@dynamic date; @dynamic primitiveDate;- (void) awakeFromInsert {[super awakeFromInsert];self.primitiveDate = [NSDate date]; }#pragma mark - #pragma mark Getter and setter- (NSInteger)processID {[self willAccessValueForKey:@"processID"];NSInteger pid = processID;[self didAccessValueForKey:@"processID"];return pid; }- (void)setProcessID:(NSInteger)newProcessID {[self willChangeValueForKey:@"processID"];processID = newProcessID;[self didChangeValueForKey:@"processID"]; }// Implement a setNilValueForKey: method. If the key is “processID” then set processID to 0.- (void)setNilValueForKey:(NSString *)key {if ([key isEqualToString:@"processID"]) {self.processID = 0;}else {[super setNilValueForKey:key];} }@end
1)這個(gè)類中的 date 和?primitiveDate 的訪問(wèn)屬性為?@dynamic,這表明在運(yùn)行期會(huì)動(dòng)態(tài)生成對(duì)應(yīng)的 setter 和 getter;
2)在這里我們演示了如何正確地手動(dòng)實(shí)現(xiàn) processID 的 setter 和 getter:為了讓 ManagedObjecContext ?能夠檢測(cè) processID的變化,以及自動(dòng)支持 undo/redo,我們需要在訪問(wèn)和更改數(shù)據(jù)對(duì)象時(shí)告之系統(tǒng),will/didAccessValueForKey 以及?will/didChangeValueForKey 就是起這個(gè)作用的。
3)當(dāng)我們?cè)O(shè)置 nil 給數(shù)據(jù)對(duì)象 processID?時(shí),我們可以在 setNilValueForKey?捕獲這個(gè)情況,并將 processID ?置 0;
4)當(dāng)數(shù)據(jù)對(duì)象被插入到 ManagedObjectContext 時(shí),我們?cè)?awakeFromInsert?將時(shí)間設(shè)置為當(dāng)前時(shí)間。
?
轉(zhuǎn)載于:https://www.cnblogs.com/oc-bowen/p/5132344.html
總結(jié)
以上是生活随笔為你收集整理的IOS-Core Data的使用的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。
- 上一篇: HTTPS_SSL配置的步骤以及原理说明
- 下一篇: linux基本工具使用(二)