Chapter 6. Creating, Listing, Displaying, and Deleting Records of Customers

In the previous chapter, we understood the meaning of the code that is automatically generated for us by Core Data in the Application Delegate files. We also developed the application that saves, lists, and deletes customer names. In this chapter, we will enhance the same application to save, list, display, and delete the information of a customer. But before we do so, we will learn 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 to apply the knowledge gained so far such as the concept of KVC, its methods used in setting and retrieving attribute values from the managed object, different methods of the NSFetchedResultsControllerDelegate protocol, and so on, to our application.

Understanding multiple managed object context

In multiple managed object context concept, we make several instances of managed object context and each instance works independently without having knowledge of what modifications are implemented in other instances. In order to keep all instances informed of different modifications performed in each context, a notification is broadcasted by a managed object context when any save or delete operation is performed on it. Like, if a managed object context completes a save operation, it broadcasts an NSNotification with the NSManagedObjectContextDidSaveNotification key.

The benefit of this notification is that any managed object context that listens to the notification NSManagedObjectContextDidSaveNotification can update its contents by invoking the method mergeChangesFromContextDidSaveNotification:. This method updates the contents of managed object context (with the managed object context that broadcasted the notification) and will also notify any observers of those changes.

Note

The notification that is broadcasted when a delete operation is performed on a managed object is: NSManagedObjectContextObjectsDidChangeNotification notification.

So, broadcasting notification is a technique to synchronize different managed objects. Every view maintains an observer to a specific key. When a notification with that key is broadcasted, the view invokes a method to handle the notification.

NSNotification

NSNotification is an object that is broadcasted by an NSNotificationCenter object to other objects. The NSNotification object, also known as notification, is received by the observers of that notification. The notification consists of a name and an object where name is a tag that identifies the notification and the object is the object that posted the notification.

Note

The methods of NSNotificationCenter that are used for posting the notification are postNotificationName:object and postNotificationName:object:userInfo.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.17.152.103