Core Data persistent data storage (2)-simple shoppingcart application using core data

Source: Internet
Author: User

From http://www.tuicool.com/articles/bEFFFj

The content of this section is long! Core data is an advanced part of iOS app development.

The following describes how to use core data to implement shoppingcart. In this example, the app does not use the Framework to automatically generateCodeYou can write all the core data-related code on your own to thoroughly understand the usage of the main classes in the core data framework.

Use xcode's single view application template to create a shoppingcart project. The class prefix is cart, as shown in.

Add coredata Framework framework

After the shoppingcart project is created, we need to add the coredata framework to the project. For detailed operation steps, refer to-develop the positioning service app-tutorial to add the core location framework process.

After adding coredata framework, Drag and Drop coredata framework to the frameworks directory of the project to make the project file look more orderly.

Create a data model)

Next, add a data model to the project, select the core data field on the left, and select the data model template on the right, as shown in:

Enter the cartmodel name and click Create.

The cartmodel. xcdatamodeld file is created in the project navigation bar.
Next, we create a group in the project and name it resources, as shown in.

Drag the cartmodel and storyboard files to the resources folder (or group) in the project. Although this operation is not necessary, it is only for orderly project file management.

In addition, create a new group in the project and name it "model class", which will be used later. Shows the project navigation bar.

Open the cartmodel file, as shown in:

Add entity and attribute, similar to data tables and data fields. Click Add entity at the bottom of the cartmodel interface to add a new entity named customer.

Select the customer object and click the + button under attrubtes to add the corresponding attribute field.

Add the customerid (integer 32), customername (string), password (string), and username (string) attribute fields step by step.

Repeat the previous steps to create the shoppingcartmaster and shoppingcartdetail entities and add the corresponding attribute fields, as shown in.

Here, based on the needs of the app demonstration, the defined entity is relatively simple.

Next, establish the relationship between these entity (relationship), click the editor style at the bottom of cartmodel, and select the graph style on the right.

Displays three entities and their attributes, a bit like three tables in the database.

[The illustration is omitted]

There are two ways to establish the relationship between entity: (1) Select shoppingcartmaster, click and hold down the Add attribute icon below until the pop-up menu appears, and select the Add relationship option.

Select the new relationship. In the data model Inspector panel window on the right, set the relevant properties of relationship, set name to detail, set destination to shoppingcartdetail, and select the to-minus relationship check box, because one shopping cart may have multiple items.

At the same time, you can select cascade for Delete rule to achieve cascading deletion. This means that when you use core data to delete an object in shoppingcartmaster, the associated shoppingcartdetail will also be deleted.
In this case, xcode has a warning, as shown in.

[The illustration is omitted]

Shoppingcart/cartmodel. xcdatamodeld/cartmodel. xcdatamodel/: Warning: consistency error: shoppingcartmaster. detail does not have an inverse; this is an advanced setting (no object can be in multiple destinations for a specific relationship)

It means that the newly created shoppingcartmaster. Detail relationship has no inverse relationship. Apple recommends that you create a reverse link for each link.

Okay! We are creating a reverse link.

Select the shoppingcartdetail object. Like the previous operation, click Add relationship. In the data model Inspector panel window on the right, set the relevant properties of relationship, and set name to master, destination is set to shoppingcartmaster, And the inverse reverse relationship is set to detail. The previously created detail relationship is automatically set as the master reverse link. You can check it.

[The illustration is omitted]

(2) another method for establishing relationship is to select the real customer in this window, hold down the control key, and drag it to the shoppingcartmaster object.
Similarly, in the data model Inspector panel window, set the relevant properties of relationship, as shown in:

[The illustration is omitted]

Follow the above drag operation to establish a relationship. Generally, the reverse relationship is automatically established, which is more convenient. Name the relation between the customer object and shoppingcartmaster object as cart, and name the relation between shoppingcartmaster and customer as customer.

After the entity relationship has been established.

Next, create the required nsmanagedobjects subclass.

Select three entity in cartmodel, and then select generate nsmanagedobjects subclass… under the Editor menu in xcode... Sub-menu items.

Save the generated subclass file in the model class folder of the shoppingcart project. Six files are created in the shoppingcart project navigation bar, including customer. H, customer. M, shoppingcartmaster. H, shoppingcartmaster. M, shoppingcartdetail. H, and shoppingcartdetail. M.

[The illustration is omitted]

The automatically generated files enable the core data framework for the project. Do not directly modify these automatically generated files. These automatically generated model classes all inherit from the nsmanagedobject class-managed objects, that is, the object representation of data table records in the database.

The following is the customer. h file code. Let's take a look.

# Import <Foundation/Foundation. h>
# Import <coredata/coredata. h>

 

@ Class Shoppingcartmaster;

@ Interface Customer : Nsmanagedobject

@ Property (nonatomic, retain) nsnumber * customerid;
@ Property (nonatomic, retain) nsstring * customername;
@ Property (nonatomic, retain) nsstring * password;
@ Property (nonatomic, retain) nsstring * username;
@ Property (nonatomic, retain) shoppingcartmaster * cart;

 

@ End

Not finished yet !!!

The specific content and examples of this tutorial are included in 《Learn iOS 6 programming step by step .

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.