Teena nath Paul
Teena nath Paul

Reputation: 2259

Coredata object got removed from the relationship automatically

I am facing an issue with coredata relationship. Here is my problem scenario step wise

  1. I have two tables in core data. "ContactTable" and "GroupTable".
  2. "GroupTable" has one-to-many relationship with "ContactTable" and "ContactTable" has one-to-one relationship with "GroupTable". That means the tables have inverse relationship.
  3. "ContactTable" has some records e.g. "Name1", "Name2", "Name3" etc.
  4. Now suppose I create a group e.g. "Group1" in "GroupTable" and save all above 3 contact in the one-to-many relationship of "GroupTable" to "ContactTable".
  5. Now I create a second group e.g. "Group2" in "GroupTable" and save all above 3 contact in the one-to-many relationship of "GroupTable" to "ContactTable".

Problem : When I print the members of "Group2" from relationship of "GroupTable" all 3 records are printed, but When I print the members of "Group1" from relationship of "GroupTable", there are no records. Similarly if I keep on creating new groups with the same 3 contacts the records printed only in the last group.

Can anyone help me out in this or suggest any approach.

Upvotes: 0

Views: 40

Answers (1)

pbasdf
pbasdf

Reputation: 21536

This is a consequence of defining the relationship from ContactTable to GroupTable as "to-one": each Contact can be related to only one Group. Hence when you save the 3 Contacts to "Group2", CoreData automatically removes them from "Group1".

If you want instances of Contact to belong to both "Group1" and "Group2", then you must define the relationship from ContactTable to GroupTable as "to-many".

Upvotes: 1

Related Questions