使用 NSPredicate 剥离 coredata 上的 ToMany 关系

发布于 2024-10-20 04:29:50 字数 1056 浏览 2 评论 0原文

我有一个 coredata 表,其中包含 32 种语言的列表。每种语言与翻译都有一对多的关系,其中包含关联语言的 32 个翻译。

例如,英语为:

Language.code = @"EN";
//english translation
Language.Translation.code = @"EN";
Language.Translation.text = @"english";
//italian translation
Language.Translation.code = @"IT";
Language.Translation.text = @"inglese";
...

End French 为:

Language.code = @"FR";
//english translation
Language.Translation.code = @"EN";
Language.Translation.text = @"french";
//italian translation
Language.Translation.code = @"IT";
Language.Translation.text = @"francese";
...

依此类推,形成 32x32 的矩阵。

我在我的应用程序中使用它,但在某些时候我需要显示一个表格视图,其中包含每种语言的惯用语(英语,法语,意大利语,西班牙语,日本人,...)。 为此,我确实使用了通常的 NSFetchRequest,然后在 tableview cellForRowAtIndexPath: 上运行了一个 NSPredicate,如下所示:

@"Language.code = Language.Translation.code"

结果相当浪费内存,所以我想知道是否有一种方法可以去除我不使用的关系中的 31 个翻译我正在做 NSFetchRequest。 我尝试过这样的事情:

@"ANY LangTranslations.translationCode = code"

但这不是剥离,这只是检查! 有更好的方法吗?

I have a coredata table with a list of 32 languages. Each language has a relationship one-to-many with translations, which contains 32 translations of the associated language.

For example English would be:

Language.code = @"EN";
//english translation
Language.Translation.code = @"EN";
Language.Translation.text = @"english";
//italian translation
Language.Translation.code = @"IT";
Language.Translation.text = @"inglese";
...

End French would be:

Language.code = @"FR";
//english translation
Language.Translation.code = @"EN";
Language.Translation.text = @"french";
//italian translation
Language.Translation.code = @"IT";
Language.Translation.text = @"francese";
...

And so on to form a matrix of 32x32.

I use this through out my app but at some point I need to show a tableview with each language in his own let's say idiom (english, français, italiano, español, 日本人, ...).
To do this I did use the usual NSFetchRequest and then on tableview cellForRowAtIndexPath: I run an NSPredicate like:

@"Language.code = Language.Translation.code"

The result is quite memory wasting, so I was wondering if there is a way to strip the 31 translations in the relationship I do not use while I am doing the NSFetchRequest.
I tryed something like:

@"ANY LangTranslations.translationCode = code"

but this is not stripping, it's just checking!
Is there a better way to do it?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

悸初 2024-10-27 04:29:50

听起来你有一个像这样的数据模型:

Language{
    code:string
    translations<-->>Translation.language
}

Translation{
    code:string
    text:string
    language<<-->Language.translations
}

如果你已经有了 Language 对象,那么你不必获取相关的 Translation 对象,你只需步行的关系。对于任何特定的 Language 对象,translations 的值将返回所有相关翻译的 NSSet。获得集合后,您可以将其排序到数组中以在表格视图中显示。

更新:

来自评论:

基本上我想检索每个 Language 对象,其中仅包含 32 个翻译中的一个翻译

核心数据不是这样工作的。提取返回一个实体的实例,然后您可以遍历该实体关系以查找相关实体的其他实例。但是,您需要清楚自己真正想要什么对象。在这种情况下,您想要一个具有特定 Translations 对象的 Language 对象,还是想要一个具有特定 Language 的 Translations 对象对象?

如果是前者,第一步是使用以下谓词获取所需的 Language 对象:

NSPredicate *p=[NSPredicate predicateWithFormat:@"code=%@",languageCode];

... 这将为您提供英语 Language 对象。然后,您只需向 Language 询问适当的 Translation 对象:

NSPredicate *p=[NSPredicate predicateWithFormat:@"code=%@",languageCode];
Translation *t=[[aLanguageObj.translations filteredSetUsingPredicate:p] anyObject];

如果您必须经常这样做,并且您有 LanguageTranslation 的 NSManagedObject 子类code>Translation,您可以在 Language 类的方法中扭曲前面的代码,该方法将为您处理此问题:

-(Translation *) translationForCode:(NSString *) languageCode; 

... 并像这样使用它:

cell.textLabel.text=[[aLanguageObj translationForCode:languageCode].text];

如果您想要一个 Translation 对象与特定 Language 对象相关,那么您可以使用如下谓词对 Translation 实体运行提取:

NSPredicate *p=[NSPredicate predicateWithFormat:@"code==%@ AND langauge.text==%@",codeForTranslation,languageCode];

It sounds like you have a datamodel something like this:

Language{
    code:string
    translations<-->>Translation.language
}

Translation{
    code:string
    text:string
    language<<-->Language.translations
}

If you already have the Language object, then you don't have to fetch the related Translation objects you just have to walk the relationship. For any particular Language object, the value of translations will return a NSSet of all the related translations. Once you have the set, you can sort it into an array for display in a tableview.

Update:

From comment:

Basically I would like to retrieve each Language object with only one translation out of the 32 it has.

Core Data doesn't work like that. A fetch returns instances of one entity and then you walk that entities relationships to find other instances of related entities. However, you need to be clear on what object you really want. In this case do you want a Language object with certain Translations object/s or do you want a Translations object with a specific Language object?

If the former, the first step is to fetch the Language object you want with a predicate of:

NSPredicate *p=[NSPredicate predicateWithFormat:@"code=%@",languageCode];

... that will give you the english Language object. Then you just ask the Language for the appropriate Translation object:

NSPredicate *p=[NSPredicate predicateWithFormat:@"code=%@",languageCode];
Translation *t=[[aLanguageObj.translations filteredSetUsingPredicate:p] anyObject];

If you have to do this a lot and you have NSManagedObject subclasses for Language and Translation, you could warp the previous code up in a method on the Language class that would handle this for you:

-(Translation *) translationForCode:(NSString *) languageCode; 

... and uses it like:

cell.textLabel.text=[[aLanguageObj translationForCode:languageCode].text];

If you want a Translation object related to a specific Language object, then you would run a fetch on the Translationentity with a predicate like:

NSPredicate *p=[NSPredicate predicateWithFormat:@"code==%@ AND langauge.text==%@",codeForTranslation,languageCode];
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文