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 2. Understanding Core Data

In this book, we'll learn how to build a Sales Record Keeping System application using Core Data through a step-by-step approach. So, this chapter will give us an introduction to the following topics:

  • Core Data framework and its features

  • The data model and how it defines the structure of data in terms of entities, properties, and their relationships

  • Model-View-Controller (MVC)

  • Core Data API and its main components

  • An overview of the application that we will be building in this book and a glance at the different application views and the tasks performed when different controls in these views are selected

Core Data


Core Data is Apple's persistence framework, which is used to persist — store our application's data in a persistent store, which may be memory or a flat file database. It helps us represent our data model in terms of an object graph, establish relationships among objects, and it can also store object graphs on the disk. It also allows us to use the entities of our data model in the form of objects, that is, it maps our data into a form that can be easily stored in a database, such as SQLite, or into a flat file. Also, the Core Data reduces a lot of coding. On using Xcode's templates for Core Data applications, we automatically get the boilerplate code that does several complex tasks such as generating XML files, binary files, SQLite files automatically for us without writing a single code, allowing us to focus on the business logic of our application.

Besides this, Core Data also provides several features that are required in data manipulation, which includes filtering data, querying...

Data Model


Core Data describes the data in terms of a data model. A data model is used to define the structure of the data in terms of entities, properties, and their relationships.

Entities

Because Core Data maintains data in terms of objects, an entity is an individual data object to represent complete information of the person, item, object, and so on. For example, customer is an entity, which represents information of customers, such as name, address, e-mail ID, contact number, products purchased, date of purchase, and so on. Similarly, the product is an entity, which represents the information of a product, such as name of the product, price, weight, and so on. An entity consists of properties that are a combination of attributes and relationships. An entity in Xcode's Data Model Editor may appear as shown in the following screenshot:

Properties

Properties of an entity give detailed information about it, such as what are its attributes and how it is related to other entities. A property...

Model View Controller (MVC)


iPhone application development uses MVC architecture where M stands for Model, V stands for View, and C for Controller.

  • Model represents the backend data — data model

  • View represents the user interface elements through which the user looks at the contents displayed by the application and can interact with them

  • Controller represents the application logic that decides the type of view to be displayed on the basis of actions taken by the user

Core Data organizes the data model in terms of objects that are easy to handle and manipulate. The finalized objects are stored on a persistent storage. The usual way of representing data models is through classes that contains variables and accessor methods. We don't have to create classes by hand, (for our data models) as Core Data framework provides a special Data Model Design tool (also known as Data Model Editor) for quickly creating an entity relationship model. The terms that we will be frequently using from now onwards...

Core Data API


The Core Data API, also called the stack, consists of three main components:

  • NSPersistentStoreCoordinator

  • NSManagedObjectModel

  • NSManagedObjectContext

The PersistentStoreCoordinator plays a major role in storing and retrieving managed objects from the Persistent Store via ManagedObjectContext. We can see in the following figure how the three are related:

The Managed Object Model (an instance of NSManagedObjectModel class) is created from the data model of our application. If there is more than one data model in our application, the Managed Object Model is created by merging all of the data models found in the application bundle. The managed object (instance of the NSManagedObject class or its subclass) represents an instance of an entity that is maintained (managed) by the Core Data framework. A managed object is an instance of an Objective-C class, but it differs from other objects in three main ways:

  • A managed object must be an instance of NSManagedObject or of a class...

Overview of the application: Sales Record System for a Departmental Store


We are going to assume the end user of this app is a wholesale dealer who wants to track in-stock product quantities, sales, and customer information. In this application, the user can store and track information about customers, such as names, e-mail IDs, and phone numbers, as well as sales data and product information (name, quantity, price, photo, and inventory). Therefore, the application must store information about:

  • Products

  • Customer information

  • Sales

    Note

    From now on, we will be referring to a vendor's product as Master Product and an item sold to customers as Product.

Entity Relationship Diagram


Our application consists of three entities:

  • Customer

  • Product

  • MasterProduct

The Entity Relationship Diagram (ERD) of the application is as shown in the following figure:

Let's have a quick look at the usage of the three entities displayed in the preceding figure:

  • The Product table is used to store information about products sold to the selected customer. The table has three fields: itemname, price, and quantity. This table is related to the Customer table via a many-to-one relationship because many customers may purchase the same product.

  • The Customer table is used to store information about the customer and has three data fields: name, e-mail ID, and contact number. This table is related to the Product table via a one-to-many relationship because a customer can purchase more than one product.

  • The MasterProduct table is used to store information about the vendor's products. This table also shows the quantity on hand for each product. The table has four fields: itemname...

An application output sample


Let's take a look at how the application works. When the program is started, the initial view will appear, as shown in the following given image (a). The top of the view holds a navigation bar with two bar button items: Edit and +. At the bottom of the view is a toolbar holding a bar button item called Master Product Information. The title in the navigation bar, Customers List, tells us that the two bar button items (Edit and +) are concerned with editing and adding customer information, respectively. The bar button item, Master Product Information, is used to store product data: name, price, quantity, and the product image.

Entering Master Product Information

We begin by entering the product data, Master Product Information, which will include product name, price, quantity on hand, and a product photo. The product quantity is automatically adjusted after a sale.

When Master Product Information is selected, the view appears as shown in preceding image (b). The...

Automatic Master Product Update


The quantity sold to a customer is automatically deducted from the master product's quantity in hand. To see if the stock has been updated, first click on the Master Product Information button from the toolbar at the bottom of the main view (the first view of the application); we'll see the list of products we saved earlier, as shown in the given image (b). After selecting a master product, we'll see the page that displays the master product information, as shown in next given image (c). And we'll find that the quantity of the master product has been adjusted accordingly.

Summary


  • In this chapter, we had an introduction of what Core Data is and its features that make it a preferred framework used for persisting information.

  • We also had a brief idea of the Data Model and how it is related to entities, properties, attributes, relationships, inverse relationships, and so on.

  • We have also learned about the concept of MVC and the role of the terms: Managed Object Model, Managed Object, and Managed Object Context. Finally, we saw the Core Data API and Persistent Store, Persistent Store Coordinator, Fetch Request, and FetchedResultsController.

  • Also, we took a detailed look at the different functions that can be performed by the application we will develop in this book.

  • We saw different screenshots the variety of application views, along with their respective controls. Also, we saw the tasks performed when different view controls are selected.

In the next chapter, we'll talk about protocols — what they are, how the methods are declared in a protocol, and the role of the...

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}