Choppin Broccoli
Choppin Broccoli

Reputation: 3066

Core data one to many relationship

I have a scenario where there is one entity that can have X number of items depending on what the user adds in. I have another entity that needs a relationship to any number of the first entity. That is quite easy, simply create a one-to-many relationship from the second entity to the first.

The problem I'm having is, what if the second entity can take a multiple number of each of the first entity? I'm not sure how to go about doing this.

Example: Entity A contains: x, y, z Entity B can contain (for example): 3 of x and 7 of y. Or 8 of x, 7 of y, 2 of z. All depending on what the user chooses in the app.

Upvotes: 0

Views: 205

Answers (2)

Dan Shelly
Dan Shelly

Reputation: 6011

CoreData will not let you model a (direct) relationship where you can add the same object to a to-many relationship more than once.

In your case you have 2 options that I can think of, both require you to model a new middleware relationship entity.

Suppose you have entity A and B. You now need to create an entity AtoB where AtoB structure is:

relationships:
a : to-one relationship to A entity b : to-one relationship to B entity

  1. the first option will let you manually keep the count of object B you like in property bCount:
    1. each time a user link A to B you will increase the count of the existing relationship or create a new AtoB object with the proper values (beware duplicates)
  2. the second option is to let the user add as many AtoB relationships (no uniqueness) when he link A entity to B entity:
    1. when needed get the count for these items manually or by grouping

I would prefer the first option in most cases.

The A entity and B entity will have a to-many relationship to the AtoB entity.

You can keep the to-many relationship from B to A you already have as I'm sure it has its purpose.

Upvotes: 2

Cody Robertson
Cody Robertson

Reputation: 182

Example of Many-To-Many

In this example there is a User entity that can have multiple lists, but the lists can also have multiple users. Then the list also has tasks, but these tasks can also be apart of multiple lists.

For example

Users: 1. Jeff 2. Rose

Lists: 1. Shopping 2. Homework 3. Chores

Tasks: 1, 2, 3, 4, 5.

Exmaple

Rose -> Shopping   -> Task 1, Task 3, Task 4
     -> Chores     -> Task 2, Task 4

Jeff -> Homework   -> Task 3, Task 5
     -> Chores     -> Task 2, Task 4

EDIT

This is how you would go about displaying the different information about the users and their data.

// Fetch user 
User *user = [self getUserWithName:@"Rose"];

// Loop through lists
for (List *listObject in [user lists])
{
    // PRINT: (List Name) : (# of Tasks)
    NSLog(@"%@ : %lu", [listObject listName], (unsigned int)[[listObject tasks] count]);
}

For example this would print the following output because of the data that we gave rose for the example.

Shopping : 4 
Chores   : 2

EDIT 2:

IF you create an userId field under the entitys User and Task and create a userId for each individual user then you can filter out the Tasks based on the user using the same model as above with just a simple NSPredicate.

[NSPredicate predicateWithFormat:@"userId == %@", @"123456789"];

Upvotes: 0

Related Questions