CoreData third-party library Magicalrecord

Source: Internet
Author: User

First step: Add a third-party library, add a frame

Step Two: Create a data model and create an entity

Step three: #import "coredata+magicalrecord.h"//the header file must be added to the. pch file

Fourth Step:

Initialize the database information, write the name as needed
[Magicalrecord setupcoredatastackwithstorenamed:@ "model.db"];

Main operation

-(void) ButtonClick: (ID) sender{UIButton* Button = (UIButton *) sender; Switch(Button.tag) { Case 1:        {            //Increase//<1> Creating ObjectsStudent * Stu =[Student mr_createentity]; //<2> assigning values to member properties of an objectStu.firstname =@"Lele"; Stu.lastname=@"du"; Stu.age=@" -"; //<3> adding objects to the database//Mr_defaultcontext Creating a Context object//mr_saveonlyselfandwait to write the modified results back to the database[[Nsmanagedobjectcontext Mr_defaultcontext] mr_saveonlyselfandwait]; }             Break;  Case 2:        {            //DeleteStudent * Stu =[Student Mr_findfirst]; if(Stu) {[Stu mr_deleteentity];//remove an object from memory            }            //To delete an object from the database[[Nsmanagedobjectcontext Mr_defaultcontext] mr_savetopersistentstoreandwait]; }             Break;  Case 3:        {            //ChangeNsarray * array = [Student mr_findbyattribute:@" Age"Withvalue:@" -"];  for(Student * STRinchArray) {Str.lastname=@"Hello"; Str.firstname=@" China"; Str.age=@"Lele";                    } [[Nsmanagedobjectcontext Mr_defaultcontext] mr_savetopersistentstoreandwait]; }             Break; default:        {            //Check//<1> Search by CriteriaNsarray * array = [Student mr_findbyattribute:@" Age"Withvalue:@" -"];  for(Student * STRinchArray) {NSLog (@"%@", Str.lastname); }                       //<2> querying all data in the databaseNsarray * Array1 =[Student Mr_findall];  for(Student * STRincharray1) {NSLog (@"%@", Str.lastname); }            //<3> Querying the first data information in a databaseStudent * Stu =[Student Mr_findfirst]; NSLog (@"%@", Stu.firstname); }             Break; }}

CoreData third-party library Magicalrecord

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.