IOS: simple ORM in core data

Source: Internet
Author: User

On Weibo two days ago, I saw Liu Xin and others DISCUSSING THE ORM stored in databases on Android and IOs (Object relationship ing, that is, ing databases to objects. Most of the data to be stored in your project is relatively simple, so no ORM is used for Android or IOS. In fact, a major feature of core data is the processing of ORM.

We first design our database in the xcdatamodel file: for example, if I create a data object, there is a string type attribute name and an integer type num:

 

Select data, add a file, and select the nsmanagedobject subclass type:

Xcode will automatically map objects to our database and generate the data. h Data. M file. Data is a subclass derived from nsmanagedobject,

# Import<Foundation/Foundation. h># Import<Coredata/coredata. h>@ InterfaceData: nsmanagedobject @ property (nonatomic, retain) nsstring*Name; @ property (nonatomic, retain) nsnumber*Num;@ End
 
# Import "Data. h"@ ImplementationData @ dynamic name; @ dynamic num;@ End

We can directly use the data object where we need to store coredata. Of course, you need to import data. h first, for example, my demoCode:

-( Void ) Savedatawithname :( nsstring *) Name andnum :( Int  ) Num {appdelegate * Appdelegate = [[uiapplication sharedapplication] Delegate  ]; Nsmanagedobjectcontext * Context = Appdelegate. managedobjectcontext;  //  Save data Data * Data = [nsentitydescription insertnewobjectforentityforname: @"  Data  " Inmanagedobjectcontext: Context]; data. Name = Name; data. Num = [Nsnumber numberwithint: num]; [appdelegate savecontext];  //  Get Data Nserror * Error; nsfetchrequest * Request = [[Nsfetchrequest alloc] init]; nsentitydescription * Entity = [nsentitydescription entityforname: @"  Data  " Inmanagedobjectcontext: Context]; [Request setentity: entity]; nsarray * Results = [[context executefetchrequest: Request error :& Error] Copy];  For (Data * d In  Results) {nslog (  @"  > Date. Name: % @ data. Num: % I  "  , D. Name, [D. Num intvalue]) ;}} 

 

In fact, the previous practice (Data Storage in iPhone development: core data) is to directly use the nsmanagedobject object for access. Now, it is mapped to the corresponding object. Of course, when the database is complex, its benefits will be reflected.

These are simple ORM in IOS.

 

Reference: http://marshal.easymorse.com/archives/3377

 

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.