Search code examples
ioscore-dataentity-relationshiprelationshipsinverse

how to make core-data inverse relationship not nil both references


i have a bit of an odd problem.

So building an iOS app that uses core-data. i have a Student entity and a Session entity. student session is a one to many.

on the student entity, there is a field/relationship called "sessions" to represent the many sessions the student can have.

on the session entity, there is a field/relationship called "student" to represent the student it belongs to.

also on the student entity, there is a field/relationship called "session" to represent the current session the student is in.

the problem i'm having is that when i end a student session, and set the session property to nil ([student setSession:nil];) the student property on the session gets also nullified.

when i try to do:

[session student]

i get a null.

the only way i've able to get around this is to set set the "Inverse" type to "No Inverse" in which case nullifying doing [student setSession:nil] does not nullify [session student]. but of course i get the pretty xcode warning about a "Misconfigured Property" "Student.session should have an inverse" and "Session.student should have an inverse"

is there a way for me to keep the inverse relationship, but not nullify session.student when student.session is set nil? i tried changing the delete rule on the Student entity and Session entity to "No Action" but it doesn't seem to help.

any suggestions would be appreciated.. maybe have no inverse is the right way.. just wanted to make sure i'm doing it correctly.

thanks!


Solution

  • You could define a one-to-many relationship "sessions" from Student to Session with inverse relationship "student", and a one-to-one relationship "current_session" from Student to Session with a different inverse relationship "current_student".