6. Core Data

Lesson 6. This will look at utilising the core data available within the iPhone. This allows the data to be stored and accessed via an on-phone database, similar to sqllite. In order to describe the core data functionality we will start a new project that includes the in built framework within XCode. We will then combine our new project with some of the functionality we introduced in the previous tutorials to achieve a working, if simple, App.

In the XCode file menu select New Project and the Navigation-based Application. You should notice an option on the screen for you to select “Use Core Data for storage“. Tick the box adjacent to this, select Choose and give your new project a suitable name. As before select Build and Run to see what the template gives you. You should see a table view with Edit and + options on the top navigation bar.

You should also notice that when you look at the files generated for the project there is now a .xcdatamodel file. This is typically referred to as the managed object model. There will also be an additional framework, the CoreData.framework. Other changes include the addition of four properties in the delegate header; managedObjectModel, managedObjectContext, PersistentStoreCoordinator and applicationDocumentsDirectory. The first three properties provide access to the Core Data stack. Apple’s Core Data Tutorial provides a detailed explanation of how the stack works.

Before we make any changes to the code we will create our data model or managed object model. You can create the model programmatically, or use the Xcode modelling tool to create the model graphically. We will use the XCode tool. There are a number of ways you can add data into the model depending on your personal preference regarding menu options versus mouse clicks. Only one will be discussed here.

Select your .xcdatamodel file and you will see a graphical representation of the model. Under Entity there will be an Event. Double clicking on Event will allow you to change the entity name to whatever you wish. In order to recreate our App from the Navigation Tutorial we will change it to News. The Property of our Entity needs to contain the attributes associated with our news data so using our previous data fields we had title and description. Double clicking on the current property, (N.B. a property of “timestamp” will have been generated by the template), will allow you to change the name. The details associated with each property are shown on the right of the screen. Here you can select the type, which for title we will set to string. We will also add the description property. This can be done by selecting the + button at the bottom of the property pane. Again we will set the type to string. For the moment we will not concern ourselves with the min and max string lengths or any of the other attributes of our data.

Now we will generate the files from our data. Selecting the Title entity choose
File > New File from the XCode menu. Managed Object Class should be selected already for you. Select Next. Ensure you are happy with the location and targets. Select Next again and then Finish.

You should now have a News.h and News.m file created within your project. With the properties declared and implemented as dynamic. There is no need for the properties to be Synthesized as the core data will generate the getter and setter methods at runtime. Equally the core data will manage the memory for the data so there is no need to perform the dealloc method for the title and description.

Generating the Title class updated the model as well so you need to make sure you save the model file.

Now we will use the new class. Import the header file in RootViewController.m

#import “News.h”

In order to read and parse the news data we need to add the reader and parser files we used in the previous tutorial.

About Mark

No Comments

Be the first to start a conversation

Leave a Reply

You must be logged in to post a comment.