核心数据:管理对象和实体之间的区别?

发布于 2024-11-27 09:30:24 字数 500 浏览 2 评论 0原文

我想了解更多核心数据,为什么我们要“获取”并搜索实体,而实体位于“内部”托管对象?例如:

NSManagedObjectContext *moc = [self managedObjectContext];  
NSEntityDescription *entityDescription =
    [NSEntityDescription entityForName:@"Employee" inManagedObjectContext:moc];  
NSFetchRequest *request = [[[NSFetchRequest alloc] init] autorelease];  
[request setEntity:entityDescription];

另外,持久对象存储包含什么?如果我理解的话,持久对象存储从 sqlite 文件中获取数据,但随后它会变得有点混乱,是不是:一个实体,一个持久对象存储,一个 sqlite 文件内的数据?

感谢您的回答

保罗

i would like to understand a bit more Core Data, why do we "fetch" and search for entities while the entities are "inside" managed objects? For example :

NSManagedObjectContext *moc = [self managedObjectContext];  
NSEntityDescription *entityDescription =
    [NSEntityDescription entityForName:@"Employee" inManagedObjectContext:moc];  
NSFetchRequest *request = [[[NSFetchRequest alloc] init] autorelease];  
[request setEntity:entityDescription];

also, what does a persistent object store contain? if i understood, the persistent object store takes data from a sqlite file, but then it gets a bit confused, is it : one entity, for one persistent object store, for one data inside the sqlite file?

Thanks for your answers

Paul

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

追我者格杀勿论 2024-12-04 09:30:24

基本上这里有 5 个组件。持久存储协调器、托管对象上下文、托管对象模型、实体和托管对象。它们一起工作来提供一个对象图管理系统(请注意,Core Data 不是 ORM,因此不要这样思考)。下面是 CoreData 中与它们交互的组件和各种其他类的描述

  • NSPercientStoreCoordinator - 它处理从磁盘加载数据和从磁盘加载数据。它处理各种存储(NSPercientStore)。包含的存储类型有二进制、XML 和 SQLite。您可以编写自己的存储(使用 NSAtomicStore 和 NSIncrementalStore 类),例如,如果您有自己的文件类型(理论上您可以编写一个存储来打开 Word 或Photoshop 文件(如果需要)
  • NSEntityDescription - 实体可以被视为托管对象的“类”。它定义了托管对象应具有的任何属性 (NSAttributeDescription)、关系 (NSRelationshipDescription) 和获取的属性 (NSFetchedPropertyDescription),以及其他属性,例如应该使用的 NSManagedObject 子类
  • NSManagedObjectContext - 这是内存中的“便笺本”。您可以在其中查询对象(使用 NSFetchRequests)、创建对象、删除对象等。您可以拥有多个上下文,然后在不保存的情况下丢弃一个上下文,以丢弃不再需要的任何更改。
  • NSManagedObject - Core Data 的核心单元。这些是保存数据的模型对象。您可以在它们上设置属性、关系等。
  • NSManagedObjectModel - 这表示用于数据的数据模型,通常在 Xcode 中创建的 .mom 文件中定义。这是存储所有实体的地方。

这几乎就是全部核心数据。还有一些其他类用于进行迁移和合并

Basically there are 5 components here. A persistent store coordinator, a managed object context, a managed object model, entities and managed objects. They all work together to provide an object graph management system (note that Core Data is not an ORM so it helps not to think of it that way). Below is a description of the components and the various other classes in CoreData that interact with them

  • NSPersistentStoreCoordinator - This handles the loading of data to and from disk. It deals with various stores (NSPersistentStore). The included store types are binary, XML and SQLite. You can write your own stores (using the NSAtomicStore and NSIncrementalStore classes), for example if you have your own file type (theoretically you could write a store to open a Word or Photoshop file if you desired)
  • NSEntityDescription - An entity can be sort of thought of as the "class" of a managed object. It defines any attributes (NSAttributeDescription), relationships (NSRelationshipDescription) and fetched properties (NSFetchedPropertyDescription) that a managed object should have, as well as other properties such as the NSManagedObject subclass that should be used
  • NSManagedObjectContext - This is the in memory "scratch pad". It is where you query for objects (using NSFetchRequests), create objects, delete objects etc. You can have multiple contexts, and throw one away without saving to discard any changes you no longer need.
  • NSManagedObject - The core unit of Core Data. These are your model objects that hold your data. You set the attributes, relationships etc on them.
  • NSManagedObjectModel - This represent the data model to use for your data, which is usually defined in a .mom file created within Xcode. This is where all the entities are stored.

That's pretty much the whole of core data. There are some other classes for doing migrations and merging

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文