1. Concept introduction

When there are complex data relationships in coreData, it is difficult for a table to meet the requirements, so it is necessary to understand the associative use of multiple tables using coreData.

For example, the relationship between course schedule and chapter table: the corresponding relationship between a course and chapter is one-to-many, and a single chapter can only correspond to one course; The relationship between course schedule and teacher schedule: a course can have multiple lecturers, and a lecturer can also teach multiple courses. The relationship between them is many-to-many.

The relation between tables is established by this mutual constraint.

Assuming that a course is to be deleted, whether the chapter associated with this course is deleted; Or delete a chapter, the chapter corresponding to the course should be deleted. This involves multi-table operations, setting cascading relationships between tables. Next, the coreData setup is explained one by one

2. CoreData related Settings

1) Delete the relationship





Delete relationship Settings

A.ab-> b: the delete relationship between A and b is set to:

No Action: When A is deleted, object B remains the same but points to A non-existent object. This Action is not recommended.

Nullify: When object A is deleted, object A that object B refers to is nullified. If A and B have A one-to-many relationship, object A is removed from container B

Cascade: When object A is deleted, object B pointed by object A is also deleted.

Deny: When deleting an object A that points to the existence of object B, the operation is denied;

For example :Course_Section: Cascade, when deleting a Course, all sections associated with the Course will be deleted;

Section_Course: Nullify, when deleting a Section, the Course associated with the Section will not be deleted.

2) Object correspondence





Object correspondence

A.ab-> b: the mapping between A and b is set to:

To Many: indicates that an A object can contain more than one B object.

To One: Indicates that One OBJECT A corresponds To only One object B.

For example :Course_Section: To Many, a Course can contain multiple sections;

Section_Course: To One, a Section can only point To One Course;

So the relationship between Course and Section is 1:N (1 to many).

For example :Course_Teacher: To Many, a Course can include several teachers;

Teacher_Course: To Many, one Teacher can point To Many courses;

So the relationship between Course and Section is M:N many-to-many.

3. Process for creating multiple tables

1) First create the required entity table (see “CoreData single table create using “)





Course, Section, the Teacher table

2) Establish the relation of setting deletion, and the corresponding relation is as follows (the specific meaning of setting has been introduced in the second part of the article)





Courses _ Teachers (a course can contain more than one teacher, delete the relationship: void)




Courses _ Chapters (a lesson can contain more than one chapter, delete relationship: Cascade)




Chapter _ Courses (a chapter belongs to only one course, delete relationship: void)




Teacher _ Courses (one teacher can teach more than one course, delete relationship: void)

View the style relationship after creation:





Table relationships

3) Use the created objects to generate the corresponding files :.h and.m(see “CoreData single table creation using “)





A file created from three objects

Properties and methods are generated based on the mapping between the set objects





Section adds attributes to the course object when corresponding to a course




The corresponding chapters and teachers of the course are multiple, generating NSSet storage and generating corresponding methods

NSSet is an unordered set, you can use NSSet and NSArray as required conversion use, other use method is the same as the use of a single table to add, delete, check and change, but access multi-layer access.

NSPredicate*predict = [predicateWithFormat:@” section_courseid = %d”,courseId];

Such as: self. CourseEntity. Course_teacher. Count

. .

CoreData multi-table association has made a simple introduction to the basic operation, the specific function is still waiting for users to discover, and Apple provides NSFetchController and coreData combined use, details, will be shared next time…… (Criticism is welcome ~~)