All operations are based on core data framework related APIs, engineering needs to add coredata.framework support
1. Add nsentitydescription insertnewobjectforentityforname:inmanagedobjectcontext:
Create an entity with the Nsentitydescription factory method
Appdelegate *appdelegate = [UIApplication sharedapplication].delegate;//get nsmanagedobjectcontext Nsmanagedobjectcontext *context = [Appdelegate managedobjectcontext]; Nsmanagedobject *managedobject = Nil;managedobject = [Nsentitydescription insertnewobjectforentityforname: Youentityname Inmanagedobjectcontext:context];//youentityname is a nsstring[managedobject setValue:youKeyValue Forkey:youentitykeyname];//kvo mode assignment value-key//e.g. [ManagedObject setvalue:[name descrition] ForKey: @ " Kentitykeyname "], the entity must include Name property, and its name must be" Kentitykeyname "[Appdelegate savecontext];// Don ' t forget to save the changes
2. Deletion of the context DeleteObject:
Nsmanagedobject * DeleteObject = Youwilldeleteobject;//can Get the object from the query result, which is a nsarray getti Ng by the nsfetchrequest/*e.g. Nsfetchrequest *request = [[Nsfetchrequest alloc] Initwithentityname:kentityna Me]; Nserror *error; Nsarray *objects = [Context executefetchrequest:request error:&error];//choose one object or many objects*/ Appdelegate *delegate = [UIApplication sharedapplication].delegate; Nsmanagedobjectcontext *context = [delegate managedobjectcontext]; [Context Deleteobject:deleteobject]; [Delegate Savecontext];
3. Change
4. Check
4.1 Find all context Executefetchrequest:error:
Nsmutablearray *_array; Appdelegate *appdelegate = [UIApplication sharedapplication].delegate; Nsmanagedobjectcontext *context = [Appdelegate managedobjectcontext]; Nsfetchrequest *request = [[Nsfetchrequest alloc] initwithentityname:kentityname]; Nserror *error; Nsarray *objects = [Context executefetchrequest:request error:&error]; if (objects = = nil) { NSLog (@ "There is an error!");} else{ _array = [Nsmutablearray arraywitharray:objects];}
Core Data operation additions and deletions