iPhone CoreData 加入 [英] iPhone CoreData join

查看:21
本文介绍了iPhone CoreData 加入的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是我的核心数据模型:

this is my core data model:

我正在尝试从数据库中获取给定 category.categoryName 和 languageset.languageSetName 的所有 LanguageEntries,例如

I'm trying to get all LanguageEntries from a database for a given category.categoryName and languageset.languageSetName e.g.

NSFetchRequest* fetchRequest = [[NSFetchRequest alloc] init];

    NSEntityDescription *entity = [NSEntityDescription entityForName:@"LanguageEntry" inManagedObjectContext:del.managedObjectContext];
    [fetchRequest setEntity:entity];

NSString* predicateString = [NSString stringWithFormat:@"Category.categoryName = %@ AND LanguageSet.languageSetName = %@", 
                        @"Food", @"English####Spanish"];

fetchRequest.predicate = [NSPredicate predicateWithFormat:predicateString];

NSError *error = nil;
NSArray* objects = [del.managedObjectContext executeFetchRequest:fetchRequest error:&error];

这总是返回 0 个对象.如果我将谓词字符串设置为匹配一个关系(例如 Category.categoryName = Food 或 languageSet.languageSetName = English####Spanish),它将返回数据.

This always returns 0 objects. If I set the predicate string to match on one relationship (e.g. Category.categoryName = Food or languageSet.languageSetName = English####Spanish) it will return data.

这太令人费解了,有人能解释一下吗?

This is baffling, can anyone shed some light?

->肯

推荐答案

您不能将 Core Data 视为 SQL.Core Data 中没有加入"这样的东西.在这种情况下,您试图找到两组对象的交集.逻辑上接近连接,但不在实现细节中.而编程的魔鬼总是在细节中.

Your can't think of Core Data as SQL. There is no such thing as a "join" in Core Data. In this case, your trying to find the intersection of two sets of objects. Close to a join logically but not in the implementation details. And the programming devil is always in the details.

尝试使用逻辑等号==",如下所示:

Try using the logical equal "==" like so:

@"Category.categoryName == %@ AND LanguageSet.languageSetName == %@"

我相信这会解决您的问题.

I believe that will solve your problem.

数据模型中隐藏了一个谓词编辑器.它可以帮助您设置谓词,即使您没有将它们嵌入到模型本身的提取中.只需选择一个实体,在您的情况下为LanguageEntity",然后添加一个获取请求.将出现编辑谓词,您可以使用该对话框创建谓词.它将显示可以复制到代码中的谓词的文本版本.

The data model has a predicate editor hidden way in it. It can help you set up predicates even if you don't embed them in fetches in model itself. Just select an entity, in your case "LanguageEntity", then add a Fetch Request. The edit predicate will appear and you can use the dialog to create the predicate. It will display a textual version of the predicate that you can copy into your code.

这篇关于iPhone CoreData 加入的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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