[IOS] Core Data Code Quick Check Table

Source: Internet
Author: User

The code in this article is from: http://www.appcoda.com/introduction-to-core-data/


Students who want to learn Core Data do not miss:) Here are some of the frequently used code snippets for my personal records.

There is a entity:device with three properties: Company, name, version.


1. Get the Context method:

-(Nsmanagedobjectcontext *) managedobjectcontext {    Nsmanagedobjectcontext *context = nil;    ID delegate = [[UIApplication sharedapplication] delegate];    if ([Delegate performselector: @selector (managedobjectcontext)]) {        context = [delegate managedobjectcontext];    }    return context;}

2. Add a piece of data:

    Nsmanagedobjectcontext *context = [self managedobjectcontext];        Create a new Managed object    nsmanagedobject *newdevice = [Nsentitydescription insertnewobjectforentityforname:@ " Device "Inmanagedobjectcontext:context";    [Newdevice setValue:self.nameTextField.text forkey:@ "name"];    [Newdevice setValue:self.versionTextField.text forkey:@ "version"];    [Newdevice setValue:self.companyTextField.text forkey:@ "Company"];        Nserror *error = nil;    Save the object to persistent store    if (![ Context Save:&error]) {        NSLog (@ "Can ' t save!%@%@", error, [error localizeddescription]);    }

3. Delete a single piece of data:

Nsmanagedobjectcontext *context = [self managedobjectcontext];//Delete object from Database[context deleteobject:[ Self.devices ObjectAtIndex:indexPath.row]]; Nserror *error = nil;if (![ Context Save:&error]) {    NSLog (@ "Can ' t delete!%@%@", error, [error localizeddescription]);    return;}

4. Modify a piece of data:

Nsmanagedobjectcontext *context = [self managedobjectcontext];    Update existing Device[self.device setValue:self.nameTextField.text forkey:@ "name"]; [Self.device setValue:self.versionTextField.text forkey:@ "version"]; [Self.device setValue:self.companyTextField.text forkey:@ "Company"]; Nserror *error = nil;//Save The object to persistent Storeif (![ Context Save:&error]) {    NSLog (@ "Can ' t save!%@%@", error, [error localizeddescription]);}


5. Query a bunch of data:

Fetch the devices from persistent data storensmanagedobjectcontext *managedobjectcontext = [self managedobjectcontext] ; Nsfetchrequest *fetchrequest = [[Nsfetchrequest alloc] initwithentityname:@ "Device"];self.devices = [[ Managedobjectcontext executefetchrequest:fetchrequest Error:nil] mutablecopy];




[IOS] Core Data Code Quick Check Table

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.