Core Data Fundamentals

What Is Core Data?

Core Data is a framework that you use to manage the model layer objects in your application.You can use Core Data as a framework to save, track, modify the data in iOS apps but, Core Data is not a Database. Core Data is using SQLite as it’s persistent store but the framework is not the database. Core Data is managing the object graphs, tracking the changes in the data and many other things.

Core Data actually decreases the amount of code you write to support the model layer. Core data can manages and updates objects automatically in the memory and handle a complex graph of objects without any effect on the memory management of the device.

Managed Object Model

Core Data functionality depends on which type of schema you create to describe your application’s entities, properties and relationships between them. Core Data uses a schema called a managed object model an instance of NSManagedObjectModel. In short, the richer the model the better Core Data is helpful to support your application.

Let’s create single view iOS app with Core Data check mark selected to see basics of core data.When you create project with checked mark of core data you can see two notable changes like,

Appdelegate has persistent container for the application. That implementation creates and returns a container and have loaded the store for the application.

The new demo.xcdatamodeld acts as the model layer for the data that we want to save. We can easily add the entity, attributes and relationships from the UI as like any other relational database.

Add New Records to Core Data

The process of adding the records to Core Data has following steps:

Let’s write everything inside the ViewController. As container is set up in the AppDelegate so we need to refer that container. Let’s save data for user,

Now we have set data in managed object. It’s time to save it in database. We have to write this with do try and catch block.

Fetch Data From Core Data

To fetch objects from the database you will use an NSFetchRequest to access that data. The fetching of objects from Core Data is one of the most powerful features of this framework.

In example you start by using an NSFetchRequest that explain the data you want returned.We can fetch the data from our entity using this code.

Observe Managed object context

The Core Data framework have several APIs that allow us to implement the behavior like NSFetchedResultsController class.

If any changes taking place in a managed object context, framework itself uses notifications to notify us . Every managed object context fire three types of notifications when changes taking place:

Let’s see what notifications we can observer and how we can use them:

ContextObjectsDidChange

CoreData sends this notification when managed object in the managed object context changes. It means every time we perform any CRUD operations on a managed object, object context posts a Context ObjectsDidChange notification.

ContextWillSave

Core Data notified us when we are going to save a context with the method of Managed Object Context. This notification is posted before a save operation is performed.

ContextDidSave

This observer is similar to ContextWillSave. Only difference is that CoreData notified us after saving the context.

How to observe notification?

A view controller monitors the managed object context it has a reference to.We can observer these notifications adding an observer to Notification Center like this:

If we pass nil as last argument named object, than the view controller receives notifications of every managed object context created by the application.Otherwise it receives notification for managed object context it has a reference to.

When we notified by core data for changes, we are interested in the data stored in userInfo dictionary of notification object.The userInfo dictionary includes six important keys:

Pros and cons

Core Data focuses more on objects than the traditional table database methods. In Core Data you are storing contents of an object.Core Data help us to abstracts away a lot of the complex things which we have to do with our self, such as relationships like one-to-one or one-to-many , into a single object-oriented interface.

Core Data have a nice graphical object model editor that can help you think through your schema design. Core data:

Core Data has come a long way and it become complex to use Core Data in a multithreaded application. But it’s become easier since iOS 6. Apple added a number of useful APIs to the Core Data framework to make it easier.

Conclusion

With something like Core Data, it is very important to understand what’s happening and it depends on which type of schema you create, otherwise it can backfire very easily once your application grows and becomes more complex.We hope to have find some simple techniques to make working with managed objects easier.