IOS Core Data Example With Sample Code

On November 17, 2014, in iPhone iPad Object-C, by James Liu
apple

IOS core data is the most efficient way to save and load data in iPhone and iPad app. It is also more complicated than another ways such as NSUserDefaults or NSFileManager. The best way to help us fully understand how IOS core data works is writing an real project. In this tutorial, I will design a simple iPhone app to demonstrate the whole process of using core data in iOS app. The process includes:

  • How to design the data structure with core data
  • How to initialise the core data
  • How to add data in core data
  • How to fetch data from core data
  • How to fetch data from core data by filter

Before we start the core data tutorial, let me briefly describe this small app first. This app will store 4 people’s information by using core data framework. The information includes name, password, gender, email and address. The main features of this example app is showing people’s information and search people by name. All above tasks will be implemented by core data framework.

Design Core Data Structure in Xcode’s Data Model Design Tool

Now let’s start from creating a new iOS project with core data. In the Xcode welcome page,
1. click “Create a new Xcode project”
2. select “Single View Application”
3. fill in the project information and make sure the “Use Core Data” is ticked
4. click “next” and click “create” in the coming page
1
2
3
4

After we successfully created the new project with Core Data framework, there is a file with “xcdatamodeld” extension in the project. This is a file package which groups all versions of the managed object model which is with “xcdatamodel” file. If you have only one version, it will look like a normal file. If you have several versions of managed object model, it will look like a folder which has a small arrow in the left side.

What’s Core Data

This is the definition of Core Data on Apple developer website.

The Core Data framework provides generalized and automated solutions to common tasks associated with object life-cycle and object graph management, including persistence.

It’s very hard to understand, isn’t it? So what is Core Data exactly? As my point of view, the Core Data framework is solution to save data and load data in an efficient way.

What’s Managed Object Model

Managed object model is an instance of the NSManagedObjectModel class. It describes a schema—a collection of entities—that you use in your application. Core Data is a framework. Managed object model is the class which we are using to work on Core Data framework.

What’s Entity

Managed object model is a collection of entities. Entity looks like an value object which has its own attributes. Each entity has a class which is used to represent the entity in code level.

After we understand all these basic knowledge about Core Data, we can start to build the managed object model in Xcode. Let’s click on the “CoreDataExample.xcdatamodeld” file, and create our first entity in Xcode’s Data Model Design tool.

  • Click “Add Entity” to add a new entity.
  • Switch the “Editor Style” to graph by click “Editor Style” icon
  • Double click on the entity to change the entity name to “UserProfile”
  • Click “Add Attribute” to add new attributes
  • Double click on the attributes to change its name

5
6
7

Note: The best way to edit entity name and attributes is using “Table” editor style. You can change the attributes name and types in a simple way.

Once we finish the design of managed object model. We need to create the entities classes basing on the design. You can follow these steps to generate all entity classes automatically.

  • Right click on the file and select “New File…”
  • Choose “Core Data” section under iOS and select “NSManagedObject subclass” in the right window
  • Select the data models with entities you would like to manage. In our example, there is only one data models.
  • Select the entities you would like to manage. In our example, there is only one entity to choose.
  • Click “Next” and “Create” to generate

8
9
10
11

After we finish all steps, we will get two files, UserProfile.h and UserProfile.m. In this application, that’s what we need. In real app, the model could be very complicated than this. But all concepts are the same, you can follow the same steps to design your Managed Object Model.

In next tutorial, we will introduce one way to initialise the data model, which will simply demonstrate how to save data in managed object model with core data framework.

 

Leave a Reply

WordPress Blog

Weboy