CoreData,多对多关系和NSPredicate [英] CoreData, many-to-many relationships and NSPredicate
问题描述
我有一个包含多对多关系的CoreData数据模型。事实证明,NSPredicate不支持多对多关系。从CoreData.pdf:你只能在谓词中的关键字中有一个一对多的元素。
作为一个食谱的例子:许多食谱和许多成分。配方可以具有许多成分,其中盐可以是一种,而盐用于许多配方中。这是一个自然的多对多的关系。
建议的解决方法是什么?
CoreData是一个坏主意,我应该回去到SQLite?
我的理解是,你可以使任何多对多关系成为单独的一对多关系
您有:
食谱有很多成分。
成分有很多食谱。 p>
创建一个新的RecipeIngredient实体,以便:
Recipe有许多RecipeIngredients。
成分有很多RecipeIngredients。
RecipeIngredient有一个食谱和一个成分。
I have a CoreData datamodel that includes a many-to-many relationship. As it turns out NSPredicate does not support many-to-many relationships. From CoreData.pdf: "You can only have one to-many element in a keypath in a predicate."
As a Recipe example: Many recipes and many ingredients. A recipe can have many ingredients of which "salt" can be one, while "salt" is used in many recipes. This is a natural many-to-many relationship.
What are suggested work-arounds?
Was CoreData a bad idea and I should go back to SQLite?
My understanding is that you can make any many-to-many relationship into separate one-to-many relationships by adding an intermediate entity.
You have:
Recipe has many Ingredients.
Ingredient has many Recipes.
Create a new RecipeIngredient entity such that:
Recipe has many RecipeIngredients.
Ingredients has many RecipeIngredients.
A RecipeIngredient has one Recipe and one Ingredient.
这篇关于CoreData,多对多关系和NSPredicate的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!