【转】CoreData以及MagicalRecord (二)

mac2022-06-30  39

3. 运行时类与对象

NSManagedObject

Managed Object 表示数据文件中的一条记录,每一个Managed Object在内存中对应的实体(Entity)的一个数据表示。Managed Object的成员为Entity的Property所描述

 

每一个 Managed Object 都有一个全局 ID(类型为:NSManagedObjectID)。Managed Object 会附加到一个 Managed Object Context,我们可以通过这个全局 ID 在 Managed Object Context 查询对应的 Managed Object。

 

NSManagedObject 常用方法 -entity获取其 Entity-objectID获取其 Managed Object ID-valueForKey:获取指定 Property 的值-setValue: forKey:设定指定 Property 的值

 

 

> Managed Object Context - NSManagedObjectContextManaged Object Context 的作用相当重要,对数据对象进行的操作都与它有关。当创建一个数据对象并插入 Managed Object Context 中,Managed Object Context 就开始跟踪这个数据对象的一切变动,并在合适的时候提供对 undo/redo 的支持,或调用 Persistent Store Coordinato 将变化保存到数据文件中去。通常我们将 controller 类(如:NSArrayController,NSTreeController)或其子类与 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:执行 Fetch Request 并返回所有匹配的数据对象

 

 

> Persistent Store Coordinator - NSPersistentStoreCoordinator使用 Core Data document 类型的应用程序,通常会从磁盘上的数据文中中读取或存储数据,这写底层的读写就由 Persistent Store Coordinator 来处理。一般我们无需与它直接打交道来读写文件,Managed Object Context 在背后已经为我们调用 Persistent Store Coordinator 做了这部分工作。

 

NSPersistentStoreCoordinator 常用方法 -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 的存储路径

 

 

> Persistent Document - NSPersistentDocumentNSPersistentDocument 是 NSDocument 的子类。 multi-document Core Data 应用程序使用它来简化对 Core Data 的操作。通常使用 NSPersistentDocument 的默认实现就足够了,它从 Info.plist 中读取 Document types 信息来决定数据的存储格式(xml,sqlite, binary)。

 

NSPersistentDocument 常用方法 -managedObjectContext返回文档的 Managed Object Context,在多文档应用程序中,每个文档都有自己的 Context。-managedObjectModel返回文档的 Managed Object Model

 

 

四,Fetch RequestsFetch Requests 相当于一个查询语句,你必须指定要查询的 Entity。我们通过 Fetch Requests 向 Managed Object Context 查询符合条件的数据对象,以 NSArray 形式返回查询结果,如果我们没有设置任何查询条件,则返回该 Entity 的所有数据对象。我们可以使用谓词来设置查询条件,通常会将常用的 Fetch Requests 保存到 dictionary 以重复利用。

NSManagedObjectContext * context = [[NSApp delegate] managedObjectContext]; NSManagedObjectModel * model = [[NSApp delegate] managedObjectModel]; NSDictionary * entities = [model entitiesByName]; NSEntityDescription * entity = [entities valueForKey:@"Post"]; NSPredicate * predicate; predicate = [NSPredicate predicateWithFormat:@"creationDate > %@", date]; NSSortDescriptor * sort = [[NSortDescriptor alloc] initWithKey:@"title"]; 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];

在上面代码中,我们查询在指定日期之后创建的 post,并将查询结果按照 title 排序返回。

 

 

NSFetchRequest 常用方法 -setEntity:设置你要查询的数据对象的类型(Entity)-setPredicate:设置查询条件-setFetchLimit:设置最大查询对象数目-setSortDescriptors:设置查询结果的排序方法-setAffectedStores:设置可以在哪些数据存储中查询

转载于:https://www.cnblogs.com/183damon/p/5291128.html

最新回复(0)