IOS persistent core data Primary Application

Source: Internet
Author: User

Today I learned how to use core data for persistence in IOS. First of all, I want to understand this.

Core data is a stable and comprehensive persistence tool. Compared with some previous persistence tools, it does not need to archive objects, that is, serialize objects, instead, create some entities in the data model editor.

In code, you no longer use the access and modification methods, but use the key-Value Pair encoding to set attributes or reduce their values.

So where are the active regions of these managed objects? They are located in the so-called persistent database. By default, core data applications implement persistent databases as SQLite databases stored in the application documentation directory.

Although data is stored through SQLite, the classes in the framework load and save data. You are not allowed to write any SQL statements. Paste the following code.

First, I created an object named line, which contains two attributes: (INT) linenum and (string) linetext.

//////////////////////////////////////// /////////////////////////////////

-(Void) viewdidload {
Core_data_persistenceappdelegate * appdelegate = [[uiapplication sharedapplication] Delegate];
Nsmanagedobjectcontext * context = [appdelegate managedobjectcontext]; // create a context
Nsentitydescription * entitydescription = [nsentitydescription entityforname: @ "line" inmanagedobjectcontext: Context]; // create an object
Nsfetchrequest * request = [[nsfetchrequest alloc] init]; // capture the request
[Request setentity: entitydescription];
 
Nserror * error;
Nsarray * objects = [context executefetchrequest: Request error: & error];
If (Objects = nil ){
Nslog (@ "There was an error! ");
}
// Array of objects obtained by Loop
For (nsmanagedobject * oneobject in objects ){
Nsnumber * linenum = [oneobject valueforkey: @ "linenum"];
Nsstring * linetext = [oneobject valueforkey: @ "linetext"];

Nsstring * fieldname = [nsstring stringwithformat: @ "field % d", linenum];

Uitextfield * thefield = [self valueforkey: fieldname];
Thefield. Text = linetext;
}
[Request release];
 
Uiapplication * APP = [uiapplication sharedapplication];
[[Nsicationcenter center defacenter center] addobserver: Self selector: @ selector (applicationwillresignactive :) name: uiapplicationwillresignactivenotification object: app];
 
[Super viewdidload];
}

 

//////////////////////////////////////// /////////////////////////////

Core_data_persistenceappdelegate * appdelegate = [[uiapplication sharedapplication] Delegate];
Nsmanagedobjectcontext * context = [appdelegate managedobjectcontext];
 
Nserror * error;
For (INT I = 1; I <= 4; I ++ ){
Nsstring * fieldname = [nsstring stringwithformat: @ "field % d", I];
Uitextfield * thefield = [self valueforkey: fieldname];
Nsfetchrequest * request = [[nsfetchrequest alloc] init];
Nsentitydescription * entitydescription = [nsentitydescription entityforname: @ "line" inmanagedobjectcontext: Context];
[Request setentity: entitydescription];
Nspredicate * Pred = [nspredicate predicatewithformat: @ "(linenum = % d)", I];
[Request setpredicate: PRED];

Nsmanagedobject * theline = nil;
Nsarray * objects = [context executefetchrequest: Request error: & error];

If (Objects = nil ){
Nslog (@ "There was an error! ");
}

If ([Objects count]> 0 ){
Theline = [Objects objectatindex: 0];
}
Else {
Theline = [nsentitydescription insertnewobjectforentityforname: @ "line" inmanagedobjectcontext: Context]; // Insert a new object
}

[Theline setvalue: [nsnumber numberwithint: I] forkey: @ "linenum"]; // you can specify the object value.
[Theline setvalue: thefield. Text forkey: @ "linetext"];

[Request release];
}
[Context save: & error]; // if an error occurs while saving the last object, an error is returned.

 

Basic Applications hope to help beginners

Related Article

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.