CCP – common coding pattern – import plist to Core Data store

In this post, you will learn a common coding pattern (CCP) for importing data in a plist to a Core Data store.

.

This post was most recently updated in October 2011. The target audience of this post will be those who are entry- or mid-level iOS programmers.

.

Data import scenario

Apps often need “starter data”. The data is included with the app, so that the app loads and functions right after installation, and before any network access or other processing is done.

The data may get created in a document or worksheet, or extracted (screen-scraped) from web pages. From a document or worksheet, it is often easy to create the data in a property list (plist) format.

This is what we’ll do in this post – import data from a plist, and add it to a Core Data store.

.

Data shape/format

The data for this post is based on nominees for the American Music Awards, (to be) held in November 2011.

The awards are organized into categories. Nominees can be artists, or albums. Therefore, we will have two entities: Category, and Nominee.

It should be obvious that a category has a to-many relationship to a nominee. That is, a category has a number of nominees.

In addition, a nominee has a to-many relationship with a category. That is, a nominee may be eligible for awards in a number of categories.

.

Plist data

There are two plists: Categories, and Nominees.

They were created in Microsoft Excel, using data from the American Music Awards web site. In Excel, the “concatenate” function was used to create the plist strings. Each plist is an array of dictionaries. All values are simple strings.

The Categories plist has (simply) the full name of the category, and a shorter name, which will fit in an iPhone (portrait) table view. Here’s an excerpt of the Categories plist:

.

The Nominees plist has the nominee’s name, a URL string for a nominee image, and the award category. Here’s an excerpt of the Nominees plist:

.

Core Data model

Two related entities were created: Category, and Nominee. Please note the following:

  • All attributes are strings
  • All attributes are required (i.e. not “Optional”)
  • The relationships are to-many, both ways

.

Here is an image of the data model:

.

Import strategy and algorithm

First, we’ll import the category data. Then, we’ll import the nominee data.

.

Data shape/format

To

.

Data shape/format

To

.


.

.

.

Advertisements
  1. No comments yet.
  1. No trackbacks yet.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: