iOS Swift - 具有复杂数据关系的Core Data示例 [英] iOS Swift - Core Data examples with a complex data relationship

查看:97
本文介绍了iOS Swift - 具有复杂数据关系的Core Data示例的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试了解如何使用Core Data建模复杂的数据关系。



使用我的应用程序,我目前有一个Recipe,Ingredient和RecipeIngredient的实体,它将成分绑定到配方。



<我没有遇到任何从这个联合实体中获取数据的例子。如果有人可以像Swift中的RecipeIngredient那样给出一个实体的例子,我会很感激。

解决方案

你没有'的原因'看到类似于您的 RecipeIngredient 实体的示例是您不需要Core Data中的联合实体。您将Core Data视为关系数据库,您通常使用连接表来创建实体之间的有效多对多关系。如。



更新:从您的评论中,我了解您要使用中间对象添加信息关于食谱和配料之间的关系。那个 是另一个实体得到保证的情况。所以我们假设您的模型如下所示:





您似乎不太可能想直接获取其中一个 RecipeIngredient 对象;你可能只是遵循适当的关系。因此,您可以创建一个获取请求来查找名称与@chocolate cake相匹配的所有食谱。 (在文档和整个网络中都有很多使用谓词的获取请求的例子,所以我不会在这里做。)你的获取请求将返回一个我们可以调用的食谱数组 cakeRecipes ,但你可能只对一个感兴趣:

  Recipe * cake = cakeRecipes.firstObject ; 

现在,您想知道蛋糕的成分是什么?以下是成分列表:

  NSArray * ingredientNames = cake.ingredients.ingredient.name; 

如果您想记录成分名称和金额:

  for(RecipeIngredient * i in cake.ingredients){
NSLog(@%@%@,i.amount,i.ingredient.name );
}

或者,您可以使用获取请求来查找与celery匹配的成分,将结果存储在 celeries 中。之后,您可能会寻找包括芹菜在内的食谱:

  Ingredient * celery = celeries.firstObject; 
NSArray * recipes = celery.recipes.recipe

如果这没有帮助,也许你可以更具体地解决这个问题。另外,我知道你要求使用Swift,但我的手指仍然习惯于Obj-C,语言细节并没有真正发挥作用 - 核心数据在两种语言中都是一样的。


I'm trying to understand how to model complex data relationships with Core Data.

With my app, I currently have an entity of Recipe, Ingredient and RecipeIngredient which bind ingredients to a recipe.

I have not come across any example of fetching data out of this joint entity. I'd appreciate it if someone could give an example of an entity like my RecipeIngredient in Swift.

解决方案

The reason you haven't seen examples similar to your RecipeIngredient entity is that you don't need joint entities like that in Core Data. You're treating Core Data as if it were a relational database, where you'd typically use a join table in order to create efficient many-to-many relationships between entities. As explained in the Many-to-Many Relationships sub-section of the Core Data Programming Guide, with Core Data all you need to do is to specify a to-many relationship in both directions between two entities. Note the parenthetical remark in the docs:

(If you have a background in database management and this causes you concern, don't worry: if you use a SQLite store, Core Data automatically creates the intermediate join table for you.)

Here's an illustration of the relationship as you should model it, ripped straight from Xcode's model editor:

If you'd still like to see examples of how to do this, search for something like "Core Data many to many relationships" and you'll find plenty. You could start here on StackOverflow; a quick search turned up a number of examples, including How do you manage and use "Many to many" core data relationships?.

Update: From your comment, I understand that you want to use an intermediate object to add information about the relationship between recipes and ingredients. That is a case where another entity is warranted. So let's say your model looks like this:

It seems unlikely that you'd want to fetch one of these RecipeIngredient objects directly; you'd probably just follow the appropriate relationship. So, you might create a fetch request to find all the Recipes whose name matches @"chocolate cake". (There are plenty of examples of fetch requests using a predicate in the docs and all over the net, so I won't do that here.) Your fetch request will return an array of recipes that we could call cakeRecipes, but you're probably only interested in one:

Recipe *cake = cakeRecipes.firstObject;

Now, what do you want to know about the ingredients for your cake? Here's a list of the ingredients:

NSArray *ingredientNames = cake.ingredients.ingredient.name;

If you'd like to log the ingredient names and amounts:

for (RecipeIngredient *i in cake.ingredients) {
    NSLog(@"%@ %@", i.amount, i.ingredient.name);
}

Or, you could use a fetch request to find the ingredients matching "celery", storing the result in celeries. After that, you might look for recipes including celery:

Ingredient *celery = celeries.firstObject;
NSArray *recipes = celery.recipes.recipe

If this doesn't help, perhaps you could be more specific about the problem. Also, I know you asked for Swift, but my fingers are still used to Obj-C, and the language specifics don't really come into play here -- Core Data works the same in both languages.

这篇关于iOS Swift - 具有复杂数据关系的Core Data示例的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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