Core Data operation additions and deletions

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.