Timering

Purp

Core Data tutorial Part 1 / 5 - Create Managed Object Model with Attributes, Relationships & Delete Rules

In this Xcode 6, IOS 8 Core Data Tutorial, we create first of all the ManagedObjectModel. First we create a SingleView Xcode project, and set the Core Data, Entities and Relationships. Finally we set the delete rules too.

Steps

  1. Create SingleView Project using Core Data, create Core Data Model, Entities
  2. Create Entity relationships and delete rules 
  3. Attention! - Change Entity name or attributes

Part of the series:

  1. “You must to know!” Collection - Core Data simple explanations
  2. Core Data tutorial Part 2 / 5 - Save Entity’s Attributes, Relationships, and make FetchRequest
  3. Core Data tutorial Part 3 / 5 - Edit, Delete NSManagedObjects
  4. Core Data part 4 / 5 - Change Core Data model, using SortDescriptor, NSPredicate FetchRequest and filter Array using Predicate
  5. Core Data tutorial part 4.2 / 5 - Using NSFetchedResultsController, Reorder TableViewCells with LongPressGesture
  6. CoreData Tutorial Part 5/5 - Model Versioning, Lightweight Migration, Model Mapping

iCloud Sync tutorials:

  1. iCloud Sync tutorial Part 1/2 - create iCloudStore, check if user is Signed in, migrate store
  2. iCloud Sync tutorial Part 2/2 - soon!

Video tutorial

1. Create SingleView Project using Core Data, create Core Data Model, Entities

1.1. Create New Project, choose SingleView app and select “Use Core Data” checkbox.

SpotlessiCode | IOS Tutorial | Core Data tutorial | Part 1_1_1_1

SpotlessiCode | IOS Tutorial | Core Data tutorial | Part 1_1_1_2

1.2. Create Core Data model with 2 Entity: User and Type and their attributes Click on model file …

SpotlessiCode | IOS Tutorial | Core Data tutorial | Part 1_1_2_1

and then “Add Entity” button …

SpotlessiCode | IOS Tutorial | Core Data tutorial | Part 1_1_2_2

set the Entity Name as User …

SpotlessiCode | IOS Tutorial | Core Data tutorial | Part 1_1_2_3

add Name (string), BirthDate (date), and Phone (Float) attributes.

SpotlessiCode | IOS Tutorial | Core Data tutorial | Part 1_1_2_4

SpotlessiCode | IOS Tutorial | Core Data tutorial | Part 1_1_2_5

Create the same with Type Entity with typeName (string) attribute

SpotlessiCode | IOS Tutorial | Core Data tutorial | Part 1_1_2_6

Probably you will find these line there:

Probably you will find these line there:

2. Create Entity relationships and delete rules 

2.1. Create Relationship

In our model a user has only one type, but one type can contain multiple users. For example, the family type may contain the brother “Joe” as a user, and “Mom” and “Dad” as well. But Dad can not be in the type of “classmates” if it’s already in the family type. So set the relationship “one to many” type, and we call “usersoftype”.

At Type Entity add Relationship: “usersoftype”. Destination: “User”. Set the Type: “To Many”, and Delete Rule: “Cascade”. In this case if you delete this type (family), you automatically delete all users who belong to this type (brother, mother, father).

SpotlessiCode | IOS Tutorial | Core Data tutorial | Part 1_2_1_1

Create the user’s relationship, which will be “To One”, because the user can belong to only one type. So we call the Relationship: “typeofuser”, Destination: “Type”, Type: “To One”, Delete Rule: “Nullify”, Type: “To One”.

Now you can set the inverse to the Relationship also, that will be: “usersoftype”.

SpotlessiCode | IOS Tutorial | Core Data tutorial | Part 1_2_1_2

The inverse will be set at Type automatically. You can check it.

SpotlessiCode | IOS Tutorial | Core Data tutorial | Part 1_2_1_3

2.2. Core Data Relationships - “You must to know!”

Check my article: “You must to know! Collection - Core Data simple explanations” 3. part

2.3. Core Data Delete Rules - “You must to know!”

Check my article: “You must to know! Collection - Core Data simple explanations” 4. part

2.4. Create NSManagedObject subclass

Click in Editor menu to the “Create NSManagedObject subclass…. menu, …

SpotlessiCode | IOS Tutorial | Core Data tutorial | Part 1_2_4_1

SpotlessiCode | IOS Tutorial | Core Data tutorial | Part 1_2_4_2

Select the Type and User Entity too!

SpotlessiCode | IOS Tutorial | Core Data tutorial | Part 1_2_4_3

the result:

SpotlessiCode | IOS Tutorial | Core Data tutorial | Part 1_2_4_4

then create a New Group called “CoreData”, and drag&drop the core data files there.

SpotlessiCode | IOS Tutorial | Core Data tutorial | Part 1_2_4_5

Finally:

SpotlessiCode | IOS Tutorial | Core Data tutorial | Part 1_2_4_6

2.5. Core Data Managed Object, Managed Object Model, and ManagedObjetcContext - “You must to know!”

Check my article: “You must to know! Collection - Core Data simple explanations” 1. part

Probably you will find these line there:

3. Attention! - Change Entity name or attributes

When you change the Entity name, you should:

  • delete the subclass files,
  • clean the App, (Product menu clean submenu)
  • and in IOS Simulator delete the App, if it’s a “live App” in Appstore you must do merging and version control… (we will do in the tutorial)
  • after that, you can create again the NSManagedObject subclass. 

Download

Download the Xcode template from Github

NEXT

The next part of the series:
Core Data tutorial Part 2 / 5 - Save Entity’s Attributes, Relationships, and make FetchRequest Check my article: “You must to know! Collection - Core Data simple explanations”

Share & Follow

If you would like to be notified of my new solutions, please subscribe to my Newsletter or my youtube channel and/ or follow me on Facebook or Twitter! Check Footer section for these opportunities!