Brief introduction
Core Data is a framework (not a database) that enables developers to manipulate data as objects without worrying about how the data is stored on disk. This is useful for IOS programmers because we can already manipulate objects that are very familiar to the code. The data objects provided by core data are called managed objects (Managed object), while core data itself is located in your application and persisted storage (persistent store) Between. To map data from a managed object to a persisted store ,Core data needs to use the managed object Model . All managed objects must reside inside the managed object Context (Managed object contexts), and the managed object context is located in the high-speed volatile memory, which is in RAM.
Why do I need a managed object context? One reason is that there is a cost to transferring data between disk and RAM. Disk reads and writes more slowly than RAM, so it should not be accessed frequently. With the managed object Context , you can get it very quickly. However, the disadvantage is that the developer must periodically invoke the save method on the managed object Context to write the changed data back to disk. Another function of the managed object Context is to record the developer's modifications to the managed object to provide complete undo and redo support.
We have a brief introduction to Core Data above. Next we need to explain the important nouns in the coredata .
Persistent Storage Coordinator
The Persistent Storage Coordinator (persistent store coordinator) contains a persistent storage area that contains several rows of data from the data table. When setting up a persistent storage coordinator, we usually use the SQLite database as a persistent store. In addition, persistent storage areas such as binary, XML, or in-memory can also be selected. Note, however, that binary and XML format stores are atomic, meaning that even if you only want to modify a small amount of data, you still need to write the entire file to disk when you save it.
The same persistence storage Coordinator can have multiple persisted stores . This can happen when you integrate CoreData with iCloud . We can put the data that doesn't belong to icloud in one store, and put icloud data in another store, which can save network bandwidth and save icloud storage space.
Even if you have two persisted stores , it doesn't mean that you have to use two object graphs . CoreData's model configuration allows developers to use multiple independent stores, but with the same set of object graphs. When you set the model configuration options for CoreData, you can indicate which persisted store a part of the object graph belongs to.
To create a persisted store , build nspersistentstore; To create a persisted storage coordinator , you need to generate an instance of the Nspersistentstorecoordinator class.
Managed Object Model
managed Object Model it resides directly between the persisted storage coordinator and the managed object Context . As implies, the managed object Model is the model or diagram describing the data structure, and the managed object is based on it. You can use Xcode to configure the relationship between entities and entities . The entities themselves do not contain data, they only specify what the attributes of managed objects based on that entity are. Entities also have attributes, and the data type of the property can be an integer, a string, or a date.
To create a managed object model , you need to generate an instance of the Nsmanagedobjectmodel class.
Managed Object Context
A managed object context can contain multiple managed objects. The managed object context is responsible for managing the life of the object, and is responsible for providing many powerful features.
The managed Object Context can also be more than one, and sometimes we need to handle tasks in the background (for example, saving data to disk or importing data), in which case multiple contexts can be used. Adding a call to save on the foreground context may cause a lag in the user interface, especially if the data changes significantly. An easy way to avoid this problem is to call save only when the user presses the home button of the phone, and the application moves to the background. A slightly more complex but flexible approach is to take two managed object contexts . Keep in mind that the managed object context is stored in high-speed memory. You can configure one of these contexts so that data from the background context can be stored asynchronously to disk. This segmented approach ensures that disk writes do not affect the smoothness of the user interface.
To create a managed object context, you need to generate an instance of the Nsmanagedobjectcontext class
What is CoreData?