2

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?

Lorenzo B
  • 33,216
  • 24
  • 116
  • 190
William Falcon
  • 9,813
  • 14
  • 67
  • 110

1 Answers1

18

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).

Community
  • 1
  • 1
Lorenzo B
  • 33,216
  • 24
  • 116
  • 190
  • So core data adds as many ingredients as you want? It automatically associates with the recipe, so i dont need a recipe ID? – William Falcon Dec 28 '12 at 16:52
  • 1
    @waf04 you don't need an id in this case – Lorenzo B Dec 28 '12 at 16:53
  • Also what is cascade as opposed to nullify? – William Falcon Dec 28 '12 at 16:55
  • @waf04 documentation is your friend ;) https://developer.apple.com/library/mac/#documentation/Cocoa/Conceptual/CoreData/Articles/cdRelationships.html – Lorenzo B Dec 28 '12 at 16:57
  • I notice they have many to many here. Would it make sense to ise many to many for ingredient to recipe? If ten recipes have ingredient1 as an ingredient, would it save room? – William Falcon Dec 28 '12 at 17:10
  • @waf04 yes. You could also have many-to-many. See, what I wrote in my answer *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.* – Lorenzo B Dec 28 '12 at 17:13
  • I´ve been looking for an explanation like this for a long time, excellent flexaddicted...simple and excellent!, i learn about relationships...in my case i have 3 entities and they are not suppose to have common property´s, but i will have a save and a load button, so i should wrap all the entities into an object right? and create the relationships like you explain here? thanks – Japa Jul 06 '13 at 00:23
  • @Japa Happy to help ;) Could you explain better your goal? Maybe ask a question in SO and put the link to it here. Maybe I can help somehow. – Lorenzo B Jul 06 '13 at 17:32
  • Hello flexaddicted, could you see this post please: http://stackoverflow.com/questions/17498307/core-data-inheritance-and-relationships – Japa Jul 06 '13 at 22:27
  • @LorenzoB What is the code one would use to associate the ingredient with the recipe in this example? – Adrian Nov 27 '17 at 18:05