如何在Coredata中设置NSPredicate和NSEntityDescription进行复杂的SQL查询 [英] How to setup NSPredicate and NSEntityDescription in Coredata to do complicated SQL query

查看:55
本文介绍了如何在Coredata中设置NSPredicate和NSEntityDescription进行复杂的SQL查询的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我知道如何创建NSPredicate来执行 SELECT * FROM DRINK 这样的sql。但是该查询如何:

I know how to create NSPredicate to do sql like "SELECT * FROM DRINK". But how about this query:

"SELECT I.name, D_I.amount 
FROM DRINK_INGREDIENT D_I, DRINK, INGREDIENT I 
WHERE D_I.drinkID=1 AND DRINK.drinkID=1 AND I.ingredientID = D_I.ingredientID;"  

如何为这种查询设置NSEntityDescription和NSPredicate?

How do I even set the NSEntityDescription, and NSPredicate for this kind of query?

推荐答案

通常,对于Core Data,您将设计并使用满足您需要的数据模型。您不应该从SQL的角度考虑它-它甚至不必使用SQL进行存储-也不要尝试直接将SQL查询转换为带有谓词的提取请求等。

Typically with Core Data you will design and use a data model that meets your needs. You should not think about it in terms of SQL--it does not even have to use SQL for storage--nor should you try to directly translate a SQL query into a fetch request with a predicate, etc.

也就是说,在构建谓词时考虑使用SQL WHERE子句有时会有所帮助,但实际上获取请求取决于您需要获取的实体以及应如何过滤集合以及/或排序等。

That said, it can sometimes be helpful to think in terms of SQL WHERE clauses when building predicates, but really fetch requests comes down to what entity you need to get and how the collection should be filtered and/or sorted, etc.

获取请求仅限于一个实体,因此我认为您将需要多个获取请求来模拟查询。

Fetch requests are limited to a single entity, so I think you would need multiple fetch requests to simulate your query.

您的Core Data数据模型是什么样的?你想达到什么目的?到目前为止,您尝试了什么?

What does your Core Data data model look like? What are you trying to accomplish? What have you tried so far?

更新

听起来您的数据模型包括两个实体:Drink和Ingredient它们之间具有多对多关系:

UPDATE
It sounds like your data model includes two entities: Drink and Ingredient with a many-to-many relationship between them:

饮料<<->>配料

Drink <<-->> Ingredient

请注意,在核心数据中,除非您明确创建它,否则没有DrinkIngredient实体(有一个用于多对多关系的附加表,但该表是从您那里抽象出来的)。由于您需要与附加表中的行关联的金额值,因此建议您在核心数据中添加DrinkIngredient实体:

Note that in Core Data, there is no DrinkIngredient entity unless you explicitly create it (there is an additional table for the many-to-many relationship, but it's abstracted away from you). Since you want an amount value associated with the rows in the additional table, I would recommend adding a DrinkIngredient entity in Core Data:

Drink<->> DrinkIngredient< ;<->配料

Drink <-->> DrinkIngredient <<--> Ingredient

注意:DrinkIngredient恰好含有一种饮料和一种配料。饮料可以含有多种DrinkIngredients,而成分可以由许多DrinkIngredients使用。

Note: a DrinkIngredient has exactly one Drink and one Ingredient. Drinks can have many DrinkIngredients and Ingredients can be used by many DrinkIngredients.

听起来,您想获取特定饮料的成分名称和名称。为此,只需使用以下过滤谓词获取DrinkIngredient对象:

It sounds like you want to get the name and amount for the list of ingredients for a particular drink. To do this, simply fetch DrinkIngredient objects with a filter predicate like this:

// assuming "aDrink" is a reference to a particular Drink object
// and "drink" is the relationship from DrinkIngredient to Drink
fetchRequest.predicate = [NSPredicate predicateWithFormat:@"drink == %@",aDrink];

// if the fetch request result array is named "ingredientList"
// and the relationship from DrinkIngredient to Ingredient is "ingredient"
for (DrinkIngredient *di in ingredientList) {
    NSString *ingredientName = di.ingredient.name;
    NSUInteger amount = di.amount.integerValue;

    // use "ingredientName" and "amount" here
}

由于您使用的是Core Data,而不是SQL,因此您的操作有所不同。例如,如果要显示包含所有饮料的名称和数量的配料表,则只需获取所有Drink对象(没有过滤谓词),然后通过从Drink到DrinkIngredient的关系访问配料。

Since you are using Core Data and not SQL, you do things differently. For example, if you wanted to display an ingredient list with name and amount for all drinks, you would simply fetch all Drink objects (no filter predicate) and you access the ingredients through the relationship from Drink to DrinkIngredient.

同样,您应该考虑要尝试完成的事情以及适当地设计和使用数据模型。您不应该考虑SQL或查询。

Again, you should think about what you are trying to accomplish and design and use your data model appropriately. You should not think about SQL or queries.

这篇关于如何在Coredata中设置NSPredicate和NSEntityDescription进行复杂的SQL查询的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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