核心数据结构是否使用多个实体? [英] Core data structure use multiple entities or not?

查看:126
本文介绍了核心数据结构是否使用多个实体?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个核心数据模型,我试图找出如何构建它。



我有一个配方。它有一个名称 title image



我可以使用 recipeName title 。然后是具有 recipeName imageURL Image 实体。 p>

然后,一个成分实体与 recipename ingresient1 ingredient1measurwment ingredient2 等...



或者我会在一个食谱实体下面做(但是如果理论上我创建了一个含有100种食材的食谱,会发生什么?



,我使用 recipeName ,因为我认为你是如何链接他们的?

解决方案

p>根据您的问题,我会创建两个不同的实体。



配方成分



其中食谱成分



所以,配方会有一些属性)和一个简单的关系,例如 toIngredients toIngredients 是一个多对多的关系,换句话说,



同样, Ingredient 也有一些属性。此外,它与配方具有一对一()关系 toRecipe 。在这里,你可以决定一个对多,如果你的食谱可以共享原料,但它严格取决于你想要模型。



关系规则, toIngredients 有一个级联规则。当您删除一个食谱,其所有的成分也将被删除。相反, toRecipe 将是nullify类型。



这里是一个简单的模式。

>



其中 toIngredients 设置如下:





toRecipe 是:



p>

请注意, toRecipe 可选标志未选中。这意味着只有存在配方时,成分才可以存在。如果你不遵守这条规则,Core Data会抱怨这个。



关于图片,这取决于图片有多大。按照Marcus Zarra规则决定如何设计模型核心数据 - 存储图片(iPhone)


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?

解决方案

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.

where toIngredients is set as follows:

and toRecipe is:

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

这篇关于核心数据结构是否使用多个实体?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆