iPhone CoreData加入 [英] iPhone CoreData join

查看:131
本文介绍了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 ####西班牙语),它将返回数据。

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.

- >

推荐答案

您不能将Core Data视为SQL。在Core Data中没有诸如join这样的东西。在这种情况下,你试图找到两组对象的交集。在逻辑上关闭连接,但不在实现细节中。编程魔鬼总是在细节。

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 == %@"

这将解决你的问题。

数据模型中隐藏了一个谓词编辑器。它可以帮助您设置谓词,即使您不在模型本身的抓取中嵌入它们。只需选择一个实体(在您的情况下为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天全站免登陆