使用 NSPredicate 剥离 coredata 上的 ToMany 关系
我有一个 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
听起来你有一个像这样的数据模型:
如果你已经有了
Language
对象,那么你不必获取相关的Translation
对象,你只需步行的关系。对于任何特定的Language
对象,translations
的值将返回所有相关翻译的 NSSet。获得集合后,您可以将其排序到数组中以在表格视图中显示。更新:
来自评论:
基本上我想检索每个 Language 对象,其中仅包含 32 个翻译中的一个翻译
。核心数据不是这样工作的。提取返回一个实体的实例,然后您可以遍历该实体关系以查找相关实体的其他实例。但是,您需要清楚自己真正想要什么对象。在这种情况下,您想要一个具有特定
Translations
对象的Language
对象,还是想要一个具有特定Language 的
Translations
对象对象?如果是前者,第一步是使用以下谓词获取所需的
Language
对象:... 这将为您提供英语
Language
对象。然后,您只需向Language
询问适当的Translation
对象:如果您必须经常这样做,并且您有
Language
和Translation
的 NSManagedObject 子类code>Translation,您可以在Language
类的方法中扭曲前面的代码,该方法将为您处理此问题:... 并像这样使用它:
如果您想要一个
Translation
对象与特定Language
对象相关,那么您可以使用如下谓词对Translation
实体运行提取:It sounds like you have a datamodel something like this:
If you already have the
Language
object, then you don't have to fetch the relatedTranslation
objects you just have to walk the relationship. For any particularLanguage
object, the value oftranslations
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 certainTranslations
object/s or do you want aTranslations
object with a specificLanguage
object?If the former, the first step is to fetch the
Language
object you want with a predicate of:... that will give you the english
Language
object. Then you just ask theLanguage
for the appropriateTranslation
object:If you have to do this a lot and you have NSManagedObject subclasses for
Language
andTranslation
, you could warp the previous code up in a method on theLanguage
class that would handle this for you:... and uses it like:
If you want a
Translation
object related to a specificLanguage
object, then you would run a fetch on theTranslation
entity with a predicate like: