Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Core data structure use multiple entities or not?

I have a Core Data model and I am trying to figure out how to structure it.

Lets say I have a Recipe. it has a name, title, image and 5 ingredients.

Would I make a recipe entity with recipeName, title. Then an Image entity with recipeName, imageURL.

Then an Ingredient entity with recipename, ingresient1, ingredient1measurwment, ingredient2, etc...

Or would I do it all under a recipe entity (but what happens if theoretically i create a recipe with 100 ingredients?

Also, I use recipeName because I think thats how you link them up?

like image 645
William Falcon Avatar asked Apr 26 '26 06:04

William Falcon


1 Answers

Based on your question, I would create two different entities.

Recipe,Ingredient

where Recipe has a to-many relationship with Ingredient.

So, Recipe will have some attributes (the attributes you need) and a simple relationship called for example toIngredients. toIngredients is a to-many relationships. In other words, a recipe could have zero (or one if you want) ingredients.

In the same way, Ingredient has some attributes. In addition, it has a to one (inverse) relationship called toRecipe to its Recipe. Here you could decide also to have a to-many if your recipes could share ingredients but it strictly depends on what you want to model.

About rules for relationships, toIngredients has a cascade rule. When you delete a recipe, all its ingredients will deleted too. On the contrary, toRecipe will be of type nullify.

Here a simple schema of it.

enter image description here

where toIngredients is set as follows:

enter image description here

and toRecipe is:

enter image description here

Notice that optional flag for toRecipe is unchecked. This means an ingredient can exists only if a recipe exists. If you don't follow this rule, Core Data will complain about this.

About the image, it depends on how bigger are the images. Follow Marcus Zarra rules for deciding how to design your model Core Data - Storing Images (iPhone).

like image 174
Lorenzo B Avatar answered Apr 28 '26 22:04

Lorenzo B



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!