CoreData provides a simple method of object persistence management, so that you can not care about the storage of data, only need to care about the object's increment, delete, change, read and write.
Basic concepts
Managed objects (Managed object)
A managed object represents an object that you want to save to the data store. This is conceptually similar to a record in SQL, and usually contains fields that correspond to the properties of the object you want to save.
Datastore (data store)
Core data supports 4 types of datastores: Sqlitestore, Xmlstore, Binarystore, Inmemorystore.
The managed Object context (managed object contexts)The managed object context is similar to a buffer between the application and the data store. This buffer contains all managed objects that have not been written to the data store. You can add, delete, and change managed objects within a buffer. In many cases, when you need to read, insert, and delete objects, you will invoke the methods of the managed object context.
Persistent Storage Coordinator (persistent store coordinator)The persistent storage coordinator processes the connection to the data store and contains some underlying information, such as the name and location of the data store. This class is typically used by the managed object context.
managed Object Model (managed)The managed object model is a class that contains the definitions for each managed object that you want to store in the data store.
is probably such a hierarchical relationship:
Using CoreData
1Before you can read or write model objects to the current data store, you need to instantiate the managed object model, the managed object context, and the persisted storage coordinator. The managed object model is represented by an instance of the Nsmanagedobjectmodel class. In a project we only need to instantiate an object for all of the. xcdatamodeld files. nsmanagedobjectmodel* Managedobjectmodel = [Nsmanagedobjectmodel mergedmodelfrombundles:nil];mergedmodelfrombundle S: Search all the. xcdatamodeld files in the project and load all the entities into one Nsmanagedobjectmodel instance. This allows the managed object model to know the definition of all managed objects used in the current project
2 with the managed object Model instance, we can create a persistence coordinator instance. The persistence coordinator processes the connection to the data store. It's probably dealing with how to write objects to the data store or how to read objects from the data store.
3 with the Persistence Coordinator instance, we need to provide a data store to manage it. You can do this by sending a addPersistentStoreWithType:configuration:URL:options:error: message.
4 The final step is to instantiate the managed object context. With the managed object context, you can easily manage objects.
The following is the instance code used:
1 initialization
-(void) in viewdidload
Create the Management object model ObjectModel = [Nsmanagedobjectmodel mergedmodelfrombundles:nil]; Create persistent data store Coordinator coordinator = [[Nspersistentstorecoordinator alloc] initwithmanagedobjectmodel:objectmodel]; Add Datastore Nsarray *paths = Nssearchpathfordirectoriesindomains (NSDocumentDirectory, Nsuserdomainmask, YES); Create a SQLite database as data storage nsstring *documentsdir = [Paths objectatindex:0]; NSString *filepath = [Documentsdir stringbyappendingpathcomponent:@ "Datastore.sqlite"]; Nsurl *databaseurl = [Nsurl Fileurlwithpath:filepath]; Nserror *error = nil; [Coordinator Addpersistentstorewithtype:nssqlitestoretype Configuration:nil Url:databaseurl Options:nil error:& ERROR]; Create a Managed object context ObjectContext = [[Nsmanagedobjectcontext alloc] init]; [ObjectContext Setpersistentstorecoordinator:coordinator];
2 adding objects
nsstring* newName = Namefield.text; nsstring* newphone = Phonefield.text; nsstring* newpostcode = Postcodefield.text; Contactdata *newcontact = (Contactdata *) [nsentitydescription insertnewobjectforentityforname:@ "ContactData" Inmanagedobjectcontext:objectcontext]; Newcontact.customername = NewName; Newcontact.phonenumber = Newphone; Newcontact.postcode = Newpostcode; nserror* error; if ([ObjectContext save:&error]) { [self fetchexistingcontactdata]; [Tableofcontacts reloaddata]; }
3 Accessing objects
nsfetchrequest* fetchrequest = [[Nsfetchrequest alloc] init]; nsentitydescription* entitydescription = [nsentitydescription entityforname:@ "Contactdata" Inmanagedobjectcontext:objectcontext]; [Fetchrequest setentity:entitydescription]; nserror* error; Existingcontacts = [ObjectContext executefetchrequest:fetchrequest error:&error];
Basic use of CoreData (reprint)