Properties of relations in Core Data
In order to better learn both Cocoa and Core Data, I decided to build an application that holds cooking recipes. However, while trying to create the CD model, I already got stuck.
These are three of the classes, simplified:
Recipe:
name
ingredients
instructions
Ingredient:
name
unit (liter, teaspoon etc)
Of course, the ingredients relation in the Recipe also needs to keep track of the amount that is needed. If I were to immediately b开发者_如何学运维uild a SQL table, I'd end up with:
Recipe: Ingredient: Recipe_Ingredient:
id id recipe_id
name name ingredient_id
instr unit amount
Exactly how can I put this Recipe_Ingredient relation into a CD model? Will I simply have to add a RecipeIngredients
entity which contains these attributes?
Yes, if you want to add attributes to the relationship you need to use an intermediate entity that joins the Recipe and Ingredients entities together as well as adds the amount attribute.
If you are new to Core Data you might also find the following two links useful:
- Core Data - Relationships and Fetched Properties
- Core Data Tutorial
精彩评论