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 5. Creating, Listing, and Deleting Names of Customers

In the previous chapter, we created a Data Model and Data objects for the Customer entity. In this chapter, we will learn how to save, display, and delete a customer's information. The information of the customer will comprise of the customer's name, his/her e-mail address, and contact number, and will be stored in the Customer entity.

Recall that we created a project with the name prob in the last chapter and used the Navigation-based Application project template for doing so. The project template, as we know, provides default code in the form of auto-generated files that are required in developing the application. Let us now use the knowledge that we have gained so far to do something practical. Let us see how to create, read, update, and delete records of a customer entity.

Splitting the task into two modules


To make it easier for us to understand the procedure of maintaining the customer's information, which consists of fields such as name, emailid, and contactno, we split this task into two modules:

  • Module to save and delete customer name (that is, instead of dealing with all the three fields (name, emailid, and contactno) together, we will first learn how to handle a single field, that is, name of the customer

  • Module to save, display, and delete the complete information of the customer, that is, name, emailid, and contactno

This means that we will first learn how to handle a single attribute: customer's name. When we properly understand the procedure of saving and deleting a customer's name, we will go ahead with developing a second module that stores complete information of the customer; that includes name, e-mail ID, contact number, and so on.

Creating a module to save and delete a customer's name


In this module, we will learn how to save and edit the name of the customer.

The following are the steps involved in developing this module:

  1. 1. Adding a ViewController class: AddNameController class for adding the name of the customer.

  2. 2. Defining protocol, outlets, and action methods in header file: AddNameController.h.

  3. 3. Defining View of the AddNameController class and connecting controls.

  4. 4. Invoking delegate methods from an implementation file: AddNameController.m file.

  5. 5. Declaring delegate and implementing methods for storing name of the customers.

Using the ViewController class for adding the name of the customer

To enter the name of the customer, we need to add a View Controller class that will display a View that has a navigation bar with two navigation bar items: Save and Cancel. The View will also prompt the user to enter the customer's name to be stored in the customersdata.sqlite file (persistent store).

The View Controllers...

Key value coding (KVC)


The Core Data uses KVC to store and retrieve data from its managed objects. NSManagedObject supports the key value methods valueForKey: and setValue:forKey: for setting and retrieving attribute values from the managed object respectively.

Key value methods

The two key value methods that are widely used for setting and retrieving values of the attributes of the managed object are explained in the following sections.

The -valueForKey: method

The method -valueForKey: is a generic accessor to retrieve the specified attribute value from a managed object.

For example, let's say we have a managed object (Customer) and it has an attribute called name; we can obtain the value of the name attribute with the following:

NSString *custName = [managedObject valueForKey:@"name" ];

The value of the attribute name is retrieved from the managed object and is assigned to the NSString instance custName.

Similarly, the code used in the cellForRowAtIndexPath method in the RootViewController...

Implementing the methods of the NSFetchedResultsControllerDelegate protocol


The classes that are declared as delegate of the NSFetchedResultsControllerDelegate protocol have to implement its methods, as these methods take the necessary actions (like updating the table view) when some changes are applied to the managed object. Let us get an idea of all the four methods declared in the said protocol:

  • controllerWillChangeContent

  • controllerDidChangeContent

  • controller:didChangeObject

  • controller:didChangeSection

These four methods are defined in the protocol: NSFetchedResultsControllerDelegate. The fetched results controller monitors its managed object context and calls its delegates as changes are made to its context. These methods help in updating the table view or take other actions on the basis of modifications applied to the context.

The controllerWillChangeContent method

When the object managed by fetchedResultsController is modified, that is, if it is deleted, updated, or a new object is added...

Running the project


Now, we are ready to run the project. Let us select the Build and Go icon from the Xcode Project window. Initially, we get a blank table view (the following image (a)) with Edit and + (plus) button on the left and right side of the navigation bar respectively. On selecting the + button, the View of the AddNameController class will appear, which prompts the user to enter the name of the customer, as shown in the following given image (b). We can also see the two Bar Button Item Controls with titles Cancel and Save respectively in the navigation bar of the View of the AddNameController class. On selecting the Save button, after entering a name in Text Field control, the name will be stored in the persistent store and the table view will be refreshed (reloaded) to display the new name added, as shown in the following given image (c):

After entering a few names, our table view may appear, as shown in the following image (a). We can delete any name by selecting the corresponding...

Summary


In this chapter, we first saw the meaning of the code that is automatically generated for us by Core Data in the Application Delegate files. Then, we saw the step-by-step procedure of saving, listing, and deleting customer names. We have also learnt about the concept of KVC and its methods used in setting and retrieving attribute values from the managed object. We also saw the different methods of the NSFetchedResultsControllerDelegate protocol. In the next chapter, we will see how to implement the concept of multiple managed object contexts to our application and also how to merge the information in different managed object contexts. We will also learn how to apply the knowledge gained in this chapter to maintain a customer's information, that is, we will see step-by-step how to save, display, and delete the customer's information.

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}