Book Image

Core Data iOS Essentials

By : Bintu Harwani, B. M. Harwani
Book Image

Core Data iOS Essentials

By: Bintu Harwani, B. M. Harwani

Overview of this book

Core Data is the essential ingredient in data driven iOS apps. It's used for storing, retrieving, and manipulating application data in databases, XML, and binary formats. It's an essential component for iPhone, iPod Touch, and iPad apps.Core Data Essentials provides a clear, readable guide to the most useful aspects of Core Data. Built around a realistic example app, the book showcases the most important aspects of Core Data development in the context of a complete, functioning app written in Objective C.The book starts with a tour of how the app works. Then you'll see how to easily display data using the Table View. You'll learn how to develop an appropriate data model that fits the needs of your app, then implement that model as updatable data objects. You'll see how to update data and build relationships between objects and learn how Core Data can work with search, and how to provide your users with friendly data editing features.
Table of Contents (19 chapters)
Core Data iOS Essentials
About the Author
About the Reviewers


Since its invention, iPhone has been inspiring developers around the world to develop applications for it. Several applications can be built for iPhone, which include Web applications, Native iPhone applications, Games, Weather, News applications, Data driven applications, and so on.

Looking at the huge demand of developing data applications for iPhone inspired me to write a book on Core Data — a perfect framework for developing data-driven applications for iPhone. In this book, I have tried my level best to keep the code simple and easy to understand. I have provided step-wise instructions with screenshots at each step. Feel free to contact me at for any queries. Any suggestions for improving the book will be highly appreciated.

What this book covers

In Chapter 1, Overview, you will see a brief history of the Core Data and a small introduction to EOF and Xcode.

In Chapter 2, Understanding Core Data, you will have an introduction to the Core Data framework and its features. You will learn about the data model and how it defines the structure of data in terms of entities, properties, and their relationships. Also, you will get a brief idea of Model View Controller (MVC), the Core Data API and its main components. Besides this, the chapter includes an overview of the application (Sales Record System for a Store), which we will be building in the book along with its different views and the tasks performed when different controls in these views are selected.

Chapter 3, Understanding Objective-C Protocol and Table View, explains how an object collaborates with other objects through the delegation pattern. We will learn about the working of a protocol, that is, how a protocol and its methods are defined. We will also learn how an object adopts a protocol to act as a delegate and how a protocol establishes a contract for communication between two objects.

Chapter 4, Designing a Data Model and Building Data Objects for Customers, introduces the working of UITableView and explains step-by-step how information is displayed via the table view. The chapter explains the different methods used in displaying information through table view and also how to add more information to the existing information being displayed via table view. For instance, if four names are already being displayed in a table, this chapter will show how to add the newly entered name to the existing list of names in the table.

In Chapter 5, Creating, Listing, and Deleting Names of Customers, we will be learning how to design a data model for storing any customer's information, that is, we will define the Customer entity and its attributes. Also, we will learn to build the data object (classes) associated with the Customer entity.

Chapter 6, Creating, Listing, Displaying, and Deleting Records of Customers, focuses on explaining how a customer's information, which is stored in the Customer entity, is maintained. The chapter gives a step-by-step explanation of how to save, display, and delete the customer information that comprises customer's name, e-mail address, and contact number.

In Chapter 7, Updating and Searching Records of Customers, we will see how to modify the customer information, which is, unlike the "editing" feature in Chapter 6 that was limited to the deletion of a record; we will see how to update (modify) the information of the existing customers. The chapter explains the procedure to add an Edit button to the view, which when selected will allow us to edit the information of the selected customer. This chapter also covers how to save the modified information back to the persistent store. In this chapter, we will also learn the application of NSPredicate to apply query facility to locate the desired customer quickly.

Chapter 8, Entering, Saving, Listing, and Deleting the Records of the Products Sold to the Customers, explains how to store the information of the products purchased by different customers, we will learn to add the Product entity to our existing data model and we will see how the relationship is established from the Customer entity to the Product entity. The chapter also explains how the inverse relationship is set from the Product entity to the Customer entity and finally, the procedure of building data objects for the modified data model. Also, we will learn how to maintain the product's information, that is, how to enter, save, display, and edit the information of the products that are sold to different customers. We will learn how to develop a view to enter the product's information, develop a product's menu to add, edit, and display the products sold to the selected customer and finally, to connect the product's menu to the rest of the application.

Chapter 9, Entering, Displaying, and Deleting the Stock, covers the creation of a Master Product entity, which will be used for storing the information of products that the vendor is dealing with. All the products available for sale are stored in this table. In order to store the image of the product, the chapter explains the concept of transformable data type that is used for creating custom data types. The chapter also explains how to enter, save, display, delete, and modify the information of the master products that the vendor deals with. It also covers the working of the Image Picker control — how it is used in selecting images of the products and the different methods to define the size of the image of the product. In all, the chapter explains how to develop a view to enter master product's information, develop a menu to add, delete, and display information of master products for the MasterProduct.


The information of the products entered in the MasterProduct entity is very different from the information of the product entered in the Product entity. The Product entity stores the information of the products that are sold to the customers, whereas the MasterProduct entity refers to the products that are available for sale.

Chapter 10, Editing the Stock Information, explains how to connect the menu meant for adding, deleting, and displaying the Master Product information to the rest of the application. Also, the chapter covers the step-by-step approach to develop a view to display and modify the selected master product's information.

In Chapter 11, Displaying the Products for Sale and Updating the Stock, we will learn how to implement query facility while entering the information of the products sold to the selected customer. Also, we will see how the quantity of the master product gets automatically reduced by the amount of quantity sold to the selected customer.

Appendix, Appendix, will give you a brief idea about the topics covered in this book.

What you need for this book

This book assumes that you have an Intel-based Macintosh running Snow Leopard (Mac OS X 10.6.2 or later). Why Snow Leopard? There are many new features in Xcode that are available only on Snow Leopard. Therefore, I highly recommend upgrading to Snow Leopard, if you are using an earlier release. We will be using Xcode, an integrated development environment used for creating applications for iPad, iPhone, core data, and other Mac applications. So, download the latest version of Xcode from the following link:

The latest version of Xcode that is available at the time of this writing is Xcode 3.2.5. I have used the same version for developing the core data application in this book.

Who this book is for

This book is mainly written for those who are familiar with iPhone SDK programming and are interested in developing data-driven applications using Core Data. For understanding the concept of Core Data better, knowledge of Objective C protocol and delegation pattern is required. Besides this, familiarity with the data source patterns, such as UITableView and UITableViewDataSource, for the purpose of displaying information is also required. But never mind if you are not aware of these two concepts, Chapters 3 and 4 of the book are focused on getting you acquainted with them.


In this book, you will find a number of styles of text that distinguish between different kinds of information. Here are some examples of these styles, and an explanation of their meaning.

Code words in text are shown as follows: "The result may be either in the form of an NSArray (containing the fetched entities) or there may be an error."

A block of code is set as follows:

NSError *error;
NSArray *myArray = [myManagedObjectContext executeFetchRequest:
fetchRequest error:&error]

When we wish to draw your attention to a particular part of a code block, the relevant lines or items are set in bold:

@property(nonatomic, retain) Customer *cust;
@property(nonatomic, retain) MasterProduct *mastprod;
@property(nonatomic, retain) Product *prod;

New terms and important words are shown in bold. Words that you see on the screen, in menus or dialog boxes for example, appear in the text like this: "The view contains a toolbar with three bar button item controls — Customers List, Edit, and Add."


Warnings or important notes appear in a box like this.


Tips and tricks appear like this.

Reader feedback

Feedback from our readers is always welcome. Let us know what you think about this book — what you liked or may have disliked. Reader feedback is important for us to develop titles that you really get the most out of.

To send us general feedback, simply send an e-mail to , and mention the book title via the subject of your message.

If there is a book that you need and would like to see us publish, please send us a note in the SUGGEST A TITLE form on or e-mail .

If there is a topic that you have expertise in and you are interested in either writing or contributing to a book, see our' author guide on

Customer support

Now that you are the proud owner of a Packt book, we have a number of things to help you to get the most from your purchase.

Downloading the example code

You can download the example code files for all Packt books you have purchased from your account at If you purchased this book elsewhere, you can visit and register to have the files e-mailed directly to you.


Although we have taken every care to ensure the accuracy of our content, mistakes do happen. If you find a mistake in one of our books — maybe a mistake in the text or the code — we would be grateful if you would report this to us. By doing so, you can save other readers from frustration and help us improve subsequent versions of this book. If you find any errata, please report them by visiting, selecting your book, clicking on the errata submission form link, and entering the details of your errata. Once your errata are verified, your submission will be accepted and the errata will be uploaded on our website, or added to any list of existing errata, under the Errata section of that title. Any existing errata can be viewed by selecting your title from


Piracy of copyright material on the Internet is an ongoing problem across all media. At Packt, we take the protection of our copyright and licenses very seriously. If you come across any illegal copies of our works, in any form, on the Internet, please provide us with the location address or website name immediately so that we can pursue a remedy.

Please contact us at with a link to the suspected pirated material.

We appreciate your help in protecting our authors, and our ability to bring you valuable content.


You can contact us at if you are having a problem with any aspect of the book, and we will do our best to address it.