Core data is a way to store persistent data in IOS programming. In the previous tutorial-to read and write application settings data, we have provided three methods to store APP data:
(1) User default settings-in this case, user intervention is usually not required, such as game clearance information, video playback records, or when the app exits, you want to recover to exit.
(2) settings bundle-provides a settings application for iPhone and iPadProgramConfiguration interface.
(3) access the file system directly-read and write files that belong to the IOS File System of the current app.
In some cases, we can use core data for data persistence. Core data is not a database, but an object persistence technology provided by Apple ). The core data framework supports data change management, object storage, and object read recovery. It can use SQLite as the type of persistent storage, but it is not a database.
The tutorial will focus on introducing and demonstrating the use of the core data framework. Instead of creating a perfect application, it will demonstrate how to create an application based on the core data framework, some basic classes, tools, and technologies that need to be used.
The following describes some core classes provided by the core data framework, which must be used during app development.
Core data stack)
The term "stock" is used to express a set of core data framework objects. These objects are used to obtain data from model objects and store data to persistent store, which is also a data file. In terms of concept, a persistent store is like a database with data tables and records. One of the optional Core Data Storage types is SQLite, but the storage file is not necessarily the actual database.
Is a simple and common core data stack configuration. We usually use the managed object context at the top of the stack and the managed object context ).
Managed object and managed object context
The managed object is an nsmanagedobject object or an nsmanagedobject subclass object instance. In fact, it is the representation of the object recorded in the database data table, and the model object managed by core data. Managed Objects represent the data operated on applications, such as departments and employees in the human resources application system, and various shapes and texts in the application. A managed object is always associated with a managed object context.
The managed object context is an nsmanagedobjectcontext object instance. Context indicates a single object space in an application. It is mainly responsible for managing the managed object set. These objects constitute a group of Associated Model objects that depict one or more persistence stores ). Context is a very powerful object that plays a central role in applications. It is responsible for Lifecycle Management, verification, relationship maintenance, and revocation/Redo features.
When we create a new managed object, insert it into the context. The existing records obtained from the database will also be stored in the context as the managed object. All modifications to the managed object are kept in the memory until they are being submitted to the persistent storage to save the context ).
It depicts the managed object context, which contains two managed objects, corresponding to two records in the database. The attribute value of an object has been updated in the memory, but has not been submitted to the database for storage. In addition, there are two records in the database, and there is no corresponding managed object ).
Managed Object Model)
The managed object model is an nsmanagedobjectmodel instance. The managed object model is an object representation of the database schema and a collection of Entity description object-nsentitydescription instances.
An entity description object describes a data table entity in the database, including the table name, the name of the managed object corresponding to the table, the attributes and relationships) and so on.
Describes the relationship between object description objects, data tables in the database, and managed objects (a record corresponding to the database table.
It can be seen that each managed object has a reference to the corresponding object.
Core Data uses a model to map the Managed Objects in the application and data records in the database. It should be noted that if we modify the database schema, core data will not be able to read database records using the model we created earlier. This problem exists in many persistence mechanisms.
Let's take a look at the following examples.Code:
Customer * customer1 = (customer *) [nsentitydescription insertnewobjectforentityforname: @" Customer "
Inmanagedobjectcontext: Self. managedobjectcontext];
Customer1.customerid = [nsnumber numberwithint: 1 ];
Customer1.customername = @" Entlib.com " ;
Customer1.password = @" Entlib.com " ;
Customer1.username = @" Entlib.com " ;
The customer class is a subclass of nsmanagedobject. nsentitydescription insertnewobjectforentityforname: inmanagedobjectcontext: Creates a mer1 managed object and associates it with the managed object context self. managedobjectcontext.
Persistent store Coordinator)
Persistent store Coordinator plays an important role in the data management process of core data. However, when using the core data framework, we usually do not have to directly interact with the Coordinator. Here we will introduce the persistent Storage Coordinator in detail. If you are not interested in this, you can jump over.
Persistent Storage Coordinator is an nspersistentstorecoordinator instance that manages the Persistent object store set. A Persistent Object Storage refers to an external storage file.
Depicts the role played by the Coordinator. However, in IOS applications, we usually use a single storage (that is, a database file), so the core data stack is not as complex as it is.
The following code applies to the persistent store Coordinator application, creates an object instance of the persistent store coordinator, and sets the managed object context) persistent store coordinator ).
Nsurl * url = [[nsfilemanager defaultmanager] urlsfordirectory: nsdocumentdirectory
Indomains: nsuserdomainmask] lastobject];
Nsurl * storedatabaseurl = [url urlbyappendingpathcomponent: @" Shoppingcart Database " ];
// URL is now <document directory>/shoppingcart Database
Nserror * error = nil;
Nspersistentstorecoordinator * persistentstorecoordinator =
[[Nspersistentstorecoordinator alloc]
Initwithmanagedobjectmodel: [nsmanagedobjectmodel mergedmodelfrombundles: Nil];
If (! [Persistentstorecoordinator addpersistentstorewithtype: nssqlitestoretype
Configuration: Nil
URL: storedatabaseurl options: Nil error: & error])
{
Nslog ( @" Error while loading persistent store... % @ " , Error );
}
_ Managedobjectcontext = [[nsmanagedobjectcontext alloc] init];
[_ Managedobjectcontext setpersistentstorecoordinator: persistentstorecoordinator];
To have a deep understanding of the core data class described above, you must combine it with the actual sample program.
Core Data persistent data storage-SeriesArticle:
Core Data persistent data storage (2)-simple shoppingcart application using core data
Core Data persistent data storage (3)-write core data code
Core Data persistent data storage (4)-run the shoppingcart Application