Core Data是如何工作的

来源:互联网 发布:c语言 flag 编辑:程序博客网 时间:2024/05/18 03:25

How Core Data Works

Although you have written no code, many objects will be created to make this work. Figure 11.14 is a diagram of some of them.

Figure 11.14. Overview of Core Data

[View full size image]


So, the NSPersistentDocument reads in the model you created and uses it to create an instance ofNSManagedObjectModel. In our case, the managed object model has one NSEntityDescription, which describes our Car entity. That entity description has several instances of NSAttributeDescription.

Once it has the model, the persistent document creates an instance of NSPersistentStoreCoordinator and an instance of NSManagedObjectContext. The NSManagedObjectContext fetches instances ofNSManagedObject from the object store. While those managed objects are in memory, the managed object context observes them. Whenever the data inside the managed objects is changed, the managed object context registers the undo action with the document's NSUndoManager. The managed object context also knows which objects have been changed and need to be saved.

So, among the classes in the Core Data framework, you will find yourself interacting withNSManagedObjectContext the most. To fetch objects, you will use NSManagedObjectContext. To save changes to your object graph, you will use NSManagedObjectContext.

原创粉丝点击