1. Adding entities in. xcdatamodeld
2. Add the corresponding entity class
3. Import (Coredata.framework has selected use core Data auto-add at the beginning)
4. Initialization of response information
To load a model file from an application package
Nsmanagedobjectmodel *model = [Nsmanagedobjectmodel Mergedmodelfrombundles:nil];
Initializing persisted objects
Nspersistentstorecoordinator *per=[[nspersistentstorecoordinator Alloc]initwithmanagedobjectmodel:model];
Create Sqlight Path
NSString *path=[nssearchpathfordirectoriesindomains (NSDocumentDirectory, Nsuserdomainmask, YES) firstObject];
Nsurl *url=[nsurl fileurlwithpath:[path stringbyappendingpathcomponent:@ "Person.data"];
Adding persistent storage
Nserror *error=nil;
Nspersistentstore *store = [per addpersistentstorewithtype:nssqlitestoretype configuration:nil URL:url Options:nil error:&error];
if (store = = nil) {//Direct throw exception
[NSException raise:@ "Add Database Error" format:@ "%@", [Error localizeddescription]];
NSLog (@ "failed to open the database! ---%@ ", error.localizeddescription);
}else{
NSLog (@ "Open database successfully! ");
Initialize context, set Persistentstorecoordinator property
Self.context = [[Nsmanagedobjectcontext alloc] init];
_context.persistentstorecoordinator = per;
}
Now it is possible to make a deletion check based on context contexts.
5. Add Data
Incoming context, initializing a person
Nsmanagedobject *person=[nsentitydescription insertnewobjectforentityforname:@ "Person" InManagedObjectContext: Self.context];
[Person setvalue:@ "Zhang San" forkeypath:@ "name"];
[Person Setvalue:[nsnumber numberwithint:25] forkeypath:@ ' age ';
//
Initialize Card
Nsmanagedobject *card=[nsentitydescription insertnewobjectforentityforname:@ "card" Inmanagedobjectcontext: Self.context];
[Card setvalue:@ "1234567" forkeypath:@ "no"];
//
Create a card and person relationship
[Person Setvalue:card forkeypath:@ "card"];
2) Model mode
Person *person=[nsentitydescription insertnewobjectforentityforname:@ ' person ' inManagedObjectContext:self.context ];
person.name=@ "Zhang San";
Person.age=[nsnumber numberwithint:25];
Card *card=[nsentitydescription insertnewobjectforentityforname:@ "card" inManagedObjectContext:self.context];
card.no=@ "123456";
Person.card=card;
Saving data with context
Nserror *error=nil;
BOOL Result=[self.context save:&error];
if (result) {
NSLog (@ "Insert success");
}else{
NSLog (@ "Insert failed");
}
6. Querying data
Nsfetchrequest *fetchrequest = [[Nsfetchrequest alloc] init];
Nsentitydescription *entity = [nsentitydescription entityforname:@ "person" inManagedObjectContext:self.context];
[Fetchrequest setentity:entity];
When setting conditional filtering,% in a database SQL statement is replaced by *
Nspredicate *predicate = [nspredicate predicatewithformat:@ "name like%@", @ "Zhang"];
[Fetchrequest Setpredicate:predicate];
Sort
Nssortdescriptor *sortdescriptor = [[Nssortdescriptor alloc] initwithkey:@ "age"
Ascending:yes];
[Fetchrequest Setsortdescriptors:[nsarray Arraywithobjects:sortdescriptor, Nil]];
Nserror *error = nil;
Nsarray *fetchedobjects = [Self.context executefetchrequest:fetchrequest error:&error];
if (fetchedobjects = = nil) {
NSLog (@ "Query failed");
}else
{
For (person *obj in fetchedobjects) {
Card *car= (Card *) (Obj.card);
NSLog (@ "name:%@", car.no);
}
}
7. Delete Data
Nsfetchrequest *fetchrequest = [[Nsfetchrequest alloc] init];
Nsentitydescription *entity = [nsentitydescription entityforname:@ "person" inManagedObjectContext:self.context];
[Fetchrequest setentity:entity];
Nspredicate *predicate = [nspredicate predicatewithformat:@ "age=%@", [NSNumber numberwithint:25]];
[Fetchrequest Setpredicate:predicate];
Nserror *error = nil;
Nsarray *fetchedobjects = [Self.context executefetchrequest:fetchrequest error:&error];
if (fetchedobjects! = nil) {
For (person *obj in fetchedobjects) {
[Self.context Deleteobject:obj];
}
Nserror *error=nil;
[Self.context save:&error];
if (Error) {
NSLog (@ "Delete error");
}else {
NSLog (@ "Delete succeeded");
}
}
Simple Introduction to Core Data