Home > Mobile >  Core Data delete only the relationship link between Many-to-Many - not any objects
Core Data delete only the relationship link between Many-to-Many - not any objects

Time:07-27

This is driving me nuts and I can't find anything where someone else has asked the same question. It can't be an unusual situation.

I have a Student entity and a Classes entity and both are bi-directional

Student<<-->>Classes

A Student can be assigned to many Classes and Classes can have many students

When a Student drops out of a given class, I want to remove the link between the particular student and the class - but without deleting either the student or the class objects.

All my attempts have deleted the student or the class object.

How do I remove the relationship without removing the objects ?

CodePudding user response:

Thank You to Joakim Danielson ! The answer was there... I just wasn't aware... Core Data creates a func for the entity relationship.

As an example

entity Students has a relationship named Classes (destination = Classes entity)

entity Classes has a relationship named Students (destination = Students entity)

Core Data creates a func for each of the entity relationships:

.removeFromClasses()

.removeFromStudents()

these remove the relationship link from each of the respective relationships.

In order to remove the relationship completely, I execute both func - the entity objects both remain but are no longer linked !

CodePudding user response:

Make sure the Deletion rule for each relationship is not set to Cascade.

You can change this in the Model designer.

  • Related