Search icon
Arrow left icon
All Products
Best Sellers
New Releases
Books
Videos
Audiobooks
Learning Hub
Newsletters
Free Learning
Arrow right icon
Core Data iOS Essentials

You're reading from  Core Data iOS Essentials

Product type Book
Published in Apr 2011
Publisher Packt
ISBN-13 9781849690942
Pages 340 pages
Edition 1st Edition
Languages

Table of Contents (19) Chapters

Core Data iOS Essentials
Credits
About the Author
Acknowledgement
About the Reviewers
www.PacktPub.com
Preface
1. Overview 2. Understanding Core Data 3. Understanding Objective-C Protocol and Table View 4. Designing a Data Model and Building Data Objects for Customers 5. Creating, Listing, and Deleting Names of Customers 6. Creating, Listing, Displaying, and Deleting Records of Customers 7. Updating and Searching Records of Customers 8. Entering, Saving, Listing, and Deleting the Records of the Products Sold to the Customers 9. Entering, Displaying, and Deleting the Stock 10. Editing the Stock Information 11. Displaying the Products for Sale and Updating the Stock Appendix

Chapter 9. Entering, Displaying, and Deleting the Stock

In the previous chapter, we learnt now to add a Product entity to our existing Data Model and also how to enter and maintain the information of the product(s) sold to the selected customer. In this chapter, we will learn:

  • To create a MasterProduct entity that will be used for storing the information of products that the vendor is dealing with, that is, stock.

  • The concept of the data type: transformable that is used for creating custom data types for storing the image of the product. Also, we will be learning how Image Picker works and is used in selecting images. Besides this, we will be learning about some methods that will be used to make the image to appear within the specified size.

  • How a Value Transformer is created that is used for converting custom object into an NSData instance for saving in persistent store.

  • How to enter, save, display, delete, and modify the information of the master products that our vendor deals with.

The information...

Adding the MasterProduct entity to the Data Model


We need an extra entity to store the information of different products that a vendor is supposed to sell through this application. The information of master product includes product name, price, quantity in hand, and product image, so the entity that we are going to create will consist of four attributes: itemname, price, quantity, and image. The steps for adding the MasterProduct entity are as follows:

  1. 1. Let us open the Xcode's Data Modeling tool by double-clicking the prob.xcdatamodel file from the Resources group in Xcode Project window.

  2. 2. To add a new entity to our data model, select the + (plus) button at the bottom of the Entity pane or choose Design | Data Model | Add Entity from the menu bar. A blank entity (by default name: Entity) will be added to our data model, which we will rename to MasterProduct. Automatically, a subclass of NSManagedObject will be created for our MasterProduct entity.

  3. 3. To add attributes to our MasterProduct...

Storing the image of the MasterProduct


For storing the image of the master product, we will be assigning a special data type to the image attribute of the MasterProduct entity. That special data type is Transformable data type. So, let us have an idea of that data type before we move further.

Transformable data type

The Transformable data type is a special data type that allows us to create attributes based on an Objective-C class (custom objects). This data type is heavily used for storing instances of UIImage, UIColor, and so on. As the information stored in the persistent store has to be in the form of NSData instance, while using Transformable data type, we need to create Value Transformers to convert the custom object (information in attribute of Transformable data type) into an instance of NSData (before storing in the persistent store) and to convert the instance of NSData back to custom object while retrieving from the persistent store.

The Value Transformer Name: field

In this field...

Building the data object for the MasterProduct entity


To build the data objects (classes) for the MasterProduct entity defined in the data model, follow the given steps:

  1. 1. Click any entity in the data model editor and then choose the File | New File option. We get a dialog box to choose the template for the new file.

  2. 2. Select Cocoa Touch Classes from under the iPhone OS heading in the left-pane and select the Managed Object Class template. We get a dialog box that prompts for the location of generating the managed object class. Keeping the values as default select the Next button.

  3. 3. Check the MasterProduct entity in the dialog box that appears (there is no need for selecting the checkbox for Customer and Product's entity as their managed object classes are already created). We find two checkboxes that are already checked: Generate accessors and Generate Obj-C 2.0 Properties. Keeping the two checkboxes checked, select the Finish button.

We find two files: MasterProduct.h and MasterProduct...

Maintaining the MasterProduct information


To maintain information of the MasterProduct, we have to do four tasks:

  • Develop a View to enter the MasterProduct's information

  • Develop a MasterProduct's menu to add, delete, and display information of master products

  • Connect the MasterProduct's menu to the RootViewController class (the class that displays the first View when application is launched) so that MasterProduct's menu can be invoked from there

  • Develop a View to display and modify the selected MasterProduct's information

To execute these preceding tasks, we need to create four modules:

  • Module to develop an interface to enter information of the MasterProduct that the vendor deals with

  • Module to create a menu for manipulating the MasterProduct information. That is, we create buttons that are connected to methods to save, display, and delete MasterProduct's information

  • Module to connect the MasterProduct's menu to the rest of the project

  • Module to modify the information of the MasterProduct

For the...

Summary


In this chapter, we saw how a MasterProduct entity is added to our existing Data Model. We also saw creation of image attribute (for storing image of master product) of Transformable data type and created a Value Transformer to be used for converting an instance of UIImage into an NSData instance (while storing image to the persistent store) and for converting the NSData instance back to the UIImage instance while retrieving it from the persistent store for display. We also developed a Data Model (classes) associated with the MasterProduct entity. Also, we focused on entering and maintaining the information of the master product that our vendor deals with. The maintenance of the master product's information includes displaying, adding, deleting, and modifying the information and hence, the task is divided into four modules. We first developed a module that created a View to enter information of the master product that is available for sale. Then, to allow the user to add, save, and...

lock icon The rest of the chapter is locked
You have been reading a chapter from
Core Data iOS Essentials
Published in: Apr 2011 Publisher: Packt ISBN-13: 9781849690942
Register for a free Packt account to unlock a world of extra content!
A free Packt account unlocks extra newsletters, articles, discounted offers, and much more. Start advancing your knowledge today.
Unlock this book and the full library FREE for 7 days
Get unlimited access to 7000+ expert-authored eBooks and videos courses covering every tech area you can think of
Renews at €14.99/month. Cancel anytime}