一、基礎概念深入
1.NSManagedObjectContext
被管理數據上下文就像便箋簿
當從數據持久層獲取數據時,相當於把這些臨時的數據拷貝寫在便箋簿上,然後就可以隨心所欲的修改這些值。
通過上下文,可以對數據記錄NSManagedObject進行添加刪除更改,記錄更改後支持撤銷和重做。
除非你保存這些數據變化,否則持久層的東西是不會變化。
通常我們將 controller 類或其子類與 Managed Object Context NSManagedObjectContext綁定,這樣就方便我們動態地生成,獲取數據對象等。
常用的方法:
-save: | 將數據對象保存到數據文件 |
-objectWithID: | 查詢指定 Managed Object ID 的數據對象 |
-deleteObject: | 將一個數據對象標記爲刪除,但是要等到 Context 提交更改時才真正刪除數據對象 |
-undo | 回滾最後一步操作,這是都 undo/redo 的支持 |
-lock | 加鎖,常用於多線程以及創建事務。同類接口還有:-unlock and -tryLock |
-rollback | 還原數據文件內容 |
-reset | 清除緩存的 Managed Objects。只應當在添加或刪除 Persistent Stores 時使用 |
-undoManager | 返回當前 Context 所使用的 NSUndoManager |
-assignObject: toPersistantStore: |
由於 Context 可以管理從不同數據文件而來的數據對象, 這個接口的作用就是指定數據對象的存儲數據文件(通過指定 PersistantStore 實現) |
-executeFetchRequest: error: | 執行獲取數據請求,返回所有匹配的數據對象 |
2.NSManagedObject
被管理的數據記錄,相當於數據庫中的一條記錄
每一個NSManagedObject對象,都有一個全局 ID(類型爲:NSManagedObjectID)。每個在NSManagedObjectContext註冊過
的NSManagedObject,可以通過這個全局 ID 在上下文中查詢到。
每個在持久存儲層中的對象,都對應一個與上下文相關的NSManagedObject
常用的方法:
-entity 獲取實體
-objectID 獲取NSManagedObjectID
-valueForKey: 獲取指定 Property 的值
-setValue: forKey: 設定指定 Property 的值
3.NSFetchRequest
獲取數據的請求,通過被管理數據的上下文來執行查詢,比如
NSArray *fetchedObjects = [context executeFetchRequest:fetchRequest error:&error];
查詢時,必須指定查詢實體或實體名稱,以 NSArray 形式返回查詢結果,如果我們沒有設置任何查詢條件,則返回該 Entity 的所有數據對象。
我們可以使用謂詞來設置查詢條件,通常會將常用的 Fetch Requests 保存到 dictionary 以重複利用。
NSFetchRequest包括以下部分:
(1)實體(Entity)的名稱
(2)NSPredicate謂詞(搜索關鍵字或限定條件)
(3)排序方式(NSArray *)sortDescriptors
所有的被管理對象(managed object)都必須在上下文中註冊,而通過NSFetchRequest獲得的對象自動被註冊。
如果在上下文中已經存在了要獲取的對象,那麼這個被管理NSManagedObject將被返回。否則上下文就會從相關的數據源中查找(也可能找不到)
例如,以下代碼是查詢在指定日期之後創建的ContactInfo,並將查詢結果按照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: | 設置你要查詢的數據對象的類型(Entity) |
-setPredicate: | 設置查詢條件 |
-setFetchLimit: | 設置最大查詢對象數目 |
-setSortDescriptors: | 設置查詢結果的排序方法 |
-setAffectedStores: | 設置可以在哪些數據存儲中查詢 |
4.NSPersistentStoreCoordinator
持久化數據助理
Core Data定義了一個棧,持久化存儲助理在中間,棧頂是被管理數據的上下文,棧底是持久化存儲層,結構如圖
通常從磁盤上的數據文件中讀取或存儲數據,這些底層的讀寫就由它來處理。一般我們無需與它直接打交道,上下文已經封裝了對它的調用
常用方法:
-addPersistentStoreForURL:configuration:URL:options:error: |
加載持久化存儲數據,對應的卸載接口爲 -removePersistentStore:error: |
-migratePersistentStore:toURL:options:withType:error: |
遷移數據存儲,效果與 "save as"相似,但是操作成功後, 遷移前的數據存儲不可再使用 |
-managedObjectIDForURIRepresentation: | 返回給定 URL所指示的數據存儲的 object id,如果找不到匹配的數據存儲則返回 nil |
-persistentStoreForURL: | 返回指定路徑的 Persistent Store |
-URLForPersistentStore: | 返回指定 Persistent Store 的存儲路徑 |
5.NSManagedObjectModel
被管理的數據模型,用來描述程序的實體、其屬性、關係的模型圖
包括以下幾個部分:
(1)實體(Entity)
對應NSEntityDescription對象
相當於數據庫中的一個表
實體名稱(name)
實體類名:NSManagedObject子類的名稱
實體實例:NSManagedObject對象或其子類的實例
NSEntityDescription 常用方法:
+insertNewObjectForEntityForName:inManagedObjectContext: 工廠方法,
根據給定的 Entity 描述,生成相應的 NSManagedObject 對象,並插入 ManagedObjectContext 中。
-managedObjectClassName返回映射到 Entity 的 NSManagedObject 類名
-attributesByName以名字爲 key, 返回 Entity 中對應的 Attributes
-relationshipsByName以名字爲 key, 返回 Entity 中對應的 Relationships
(2)屬性(Property)
對應NSPropertyDescription對象
Property 爲 Entity 的特性,它相當於數據庫表中的一列,或者 XML 文件中的 value-key 對中的 key。
它可以描述實體基本屬性(Attribute),實體之間的關系(RelationShip),或查詢屬性(Fetched Property)。
<1> 實體的基本屬性(Attributes)
對應NSAttributeDescription對象
存儲基本數據,數據類型包括:
string,date,integer(NSString, NSDate, NSNumber)
<2> 實體間的關係(Relationships)
對應NSRelationshipDescription對象
支持對一、對多的關係
<3> 查詢屬性(Fetched Property)
對應NSFetchedPropertyDescription對象
根據查詢謂詞返回指定實體的符合條件的數據對象
表示了一種“弱”的、單項的關係(相當於數據庫中的查詢語句)
6.持久化存儲層(Persistent Stores)
持久化存儲層是和文件或外部數據庫關聯的,大多數訪問持久化存儲層的動作都由上下文來完成。
7.NSFetchedResultsController
用於在表視圖table view中加載部分數據
二、用代碼創建數據模型
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 = [NSComparisonPredicate predicateWithLeftExpression:lhs rightExpression:rhs modifier:NSDirectPredicateModifier type:NSGreaterThanPredicateOperatorType options: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)我們創建了一個全局模型 moModel;
2)並在其中創建一個名爲 Run 的 Entity,這個 Entity 對應的 ManagedObject 類名爲 Run(很快我們將創建這樣一個類);
3)給 Run Entity 添加了兩個必須的 Property:date 和 processID,分別表示運行時間以及進程 ID;並設置默認的進程 ID 爲 -1;
4)給 processID 特性設置檢驗條件:必須大於 0;
5)給模型設置本地化描述詞典;
本地化描述提供對 Entity,Property,Error信息等的便於理解的描述,其可用的鍵值對如下表:
Key | Value |
"Entity/NonLocalizedEntityName" | "LocalizedEntityName" |
"Property/NonLocalizedPropertyName/Entity/EntityName" | "LocalizedPropertyName" |
"Property/NonLocalizedPropertyName" | "LocalizedPropertyName" |
"ErrorString/NonLocalizedErrorString" | "LocalizedErrorString" |
三、存儲數據到xml文件
存儲類型爲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_TYPE configuration:nil URL:url options:nil error:&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; @end
Run.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)這個類中的 date 和 primitiveDate 的訪問屬性爲 @dynamic,這表明在運行期會動態生成對應的 setter 和 getter;
2)在這裏我們演示瞭如何正確地手動實現 processID 的 setter 和 getter:爲了讓 ManagedObjecContext 能夠檢測 processID的變化,以及自動支持 undo/redo,我們需要在訪問和更改數據對象時告之系統,will/didAccessValueForKey 以及 will/didChangeValueForKey 就是起這個作用的。
3)當我們設置 nil 給數據對象 processID 時,我們可以在 setNilValueForKey 捕獲這個情況,並將 processID 置 0;
4)當數據對象被插入到 ManagedObjectContext 時,我們在 awakeFromInsert 將時間設置爲當前時間。