Reputation: 211
Is there any way of adding the same object multiple times i the default CoreData one-to-many relationship?
I have one table with recipes in it, and one table with ingredients.
This is what I'm aiming at:
Apple cake
- Apple
- Apple
- Apple
- Sugar
- Flour
Other cake
- Apple
- Sugar
- Flour
- Flour
By default, the addIngredientsObject
-method only adds the ingredient if it doesn't exist in the recipe.
Do I have to create my own table with something like:
Recipe ID
Ingredient ID
Count
or is it possible with the To-Many relationship?
Upvotes: 2
Views: 1506
Reputation: 77621
I'd listen to Matthias with this.
Create a relationship like...
Recipe <->> IngredientMeasure <<-> Ingredient.
i.e. Recipe can have many ingredient measures. Ingredient measure only has one ingredient.
To answer your question though.
In a "to-many" relationship the relationship is mapped to an NSSet
. NSSet
is a unique collection of objects.
So, if you create an entity called apple
and do something like...
[myRecipe addIngredientObject:apple];
[myRecipe addIngredientObject:apple];
[myRecipe addIngredientObject:apple];
[myRecipe addIngredientObject:apple];
You will still end up with only one apple as it is adding the same item to an NSSet which will stop the duplicates.
Upvotes: 3