DPS923 notes – Thu Feb 13

Introduction to Core Data technologies.

.

Core Data is an object design, management, and persistence framework.

It helps you manage the lifecycle of objects and object graphs.

.

How do I get started?

Use a ‘project template’.

In the course GitHub repository, in the Project_Templates folder, you will see a project named ClassesV2.

Make a copy of that, and you will have all you need to get started.

How do I make a copy?

The README file in the ClassesV2 project helps you do this.

.

What’s in the “ClassesV2” project template?

The project template has all the pieces you need.

It is nicely organized.

The project template includes the Core Data ‘stack’, which provides the necessary objects and a factory/builder.

A store initializer is included, enabling you to create startup data for the app when launched for the first time.

Its Model class is configured for Core Data, and has examples of properties of methods.

And it has a table view controller, and a view controller, which can use Core Data objects.

.

Where do I see Core Data in a project?

The following screen shot shows some of the Model class in the ClassesV2 project template. (Click to view it in a new tab/window.)

Look at the following notable items:

  • CoreData.framework in the “Frameworks” group
  • The CDStack class
  • The ObjectModel file, which is the Core Data (object) model created by the model editor
  • Properties and methods in Model.m, shown in the code editor

where-is-cd-in-a-project

.

How do I design my entity objects?

Use the Core Data model editor.

The screen shot below shows an example where two entities were designed. (Click to view it in a new tab/window.)

Look at the following notable items:

  • Add Entity control
  • List of entities that have been designed
  • Add ( + ) and Remove ( – )  properties controls
  • For a selected/highlighted entity, a list of attributes and relationships
  • For a selected/highlighted entity, available settings in the Data Model Inspector (in the right-side Utility area)

how-to-design-entities

.

Naming conventions

Entity names begin with an upper-case letter. Multi-word names use camel-casing.

Property names – attribute, relationship – begin with a lower-case letter. Multi-word names use camel-casing.

Do NOT use “description” for the name of an attribute.

Why?

It’s documented in the Core Data Programming Guide, and in the NSPropertyDescription class reference document. In summary, DO NOT use these names for properties:

  • description
  • class
  • entity
  • objectID
  • self

There are a few others. An easy way to tell if you’ll have a conflict is to do the following…

In Xcode, in a Model.m method block, create a new managed object:
NSManagedObject *foo = [self addNew:@”Foo”];

Then, ‘go shopping’ by using code sense, by starting to type the name you want to use, e.g.:
id bar = [foo descript
// Xcode code sense will show you the “description” method, so you cannot use it for an attribute name!

A “to-one” relationship property name is singular. For example, “supplier”.

A “to-many” relationship property name is plural. For example, “products”.

.

After designing an entity, use Xcode to generate a custom class

After designing an entity, and adding and configuring its properties, use Xcode to generate a custom class. Here’s how:

1. Display the Core Data model editor. Select one or more entities.

2. On the Editor menu, choose “Create NSManagedObject Subclass…”. Answer the dialogs appropriately.

generate-custom-class-1

.

generate-custom-class-2

.

generate-custom-class-3

.

The result will be a class that is much more pleasurable to use when writing code.

generate-custom-class-4

.

What is a “managed object”?

“NSManagedObject is a generic class that implements all the basic behavior required of a Core Data model object.” (From the NSManagedObject class reference document.)

NSManagedObject inherits from NSObject.

In addition, we normally will use Xcode to create a custom class for an entity, which inherits from NSManagedObject. For example, if we created a “Person” entity, and generated a custom class, the inheritance hierarchy would look like this:

NSObject > NSManagedObject > Person

.

What is the “managed object context”?

“An instance of NSManagedObjectContext represents a single “object space” or scratch pad in an application. Its primary responsibility is to manage a collection of managed objects. The context is a powerful object with a central role in the life-cycle of managed objects, with responsibilities from life-cycle management … to validation, inverse relationship handling, and undo/redo.” (From the NSManagedObjectContext class reference document.)

Think of it as an in-memory “scratch pad” or temporary “work area” that you use.

.

What ‘management’ tasks can I perform on my objects?

All the tasks you would expect:

Fetch: Get all, or get one, or get some filtered, or get value. Done with a “fetch request”, introduced below.

Add: Add new object.

Edit: Edit an existing object.

Remove: Remove an existing object.

These tasks are implemented as methods in the Model class.

.

Where is the data (object graph) persisted?

In your app’s “Documents” directory.

The Core Data stack manages access to the store file. We don’t have to worry about it.

The data format of the store file is private, and is unimportant to us.

.

What is a “fetch request”?

“An instance of NSFetchRequest describes search criteria used to retrieve data from a persistent store.” (From the NSFetchRequest class documentation document.)

What “search criteria“?

  • Name of entity being searched
  • If required, a predicate (logical conditions that constrain a search)
  • If required, sort descriptors

.

What’s a “fetched results controller”?

A wonderful and awesome object.

“You use a fetched results controller to efficiently manage the results returned from a Core Data fetch request to provide data for a UITableView object.” (From the NSFetchedResultsController class reference document.)

Are you planning to use a table view? Then you will want to ‘bind’ it to a fetched results controller. The result? Happiness.

If we have only one entity in our project, we create one fetched results controller.

If we have multiple entities in our project, we create a fetched results controller for each entity.

They are created in our Model class, as properties, with custom getters.

.

How do I…

How do I define an entity object?
Use the Core Data model editor.
Add an entity, and then add and configure properties.
Finally, use Xcode to generate a custom class for the entity.

How do I write code to manage the entity?
Do most of your work in the Model class.
Create a property for the entity’s fetched results controller.
Then write methods for other fetch requests, and for handling object creation, modification, and removal.

How do I perform searches and handle results?
You can use the fetch request object in the fetched results controller if you plan to ‘bind’ the results to a table view.
Alternatively, you can use a fetch request object to do so.
In either case, results are available as an NSArray of zero-or-more objects.

How do I add, edit, and remove objects?
As noted above, write methods that handle object creation, modification, and removal in the Model class.
For ‘add’, create and configure a new object.
For ‘edit’, fetch the object. Then change its property values.
For ‘remove’, fetch the object. Then ask the context to remove it.
Always “save changes”.

.

Can I study a code example?

Yes.

The CD Types code example (in the GitHub repository) is a good example of an app that works with one entity.

You should attempt to re-create this example, using your own copy of the ClassesV2 template. Strongly recommended.

.

Show me a diagram of the objects in an iOS app that uses Core Data

In the style that we have been using in class, here is a diagram. (Click to open it full-size in a new tab/window.)

iOSAppObjectsWithCoreData

.

.

.

.

.

.

.

.

.

.

.

.

.

  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: