0

我是 Core data 的新手,我的搜索遇到了一些麻烦。

我的数据结构如下所示:

Server   --one-to-many-->   Category   --many-to-many-->   Cube

我的想法是获取服务器对象,抓取 server.category 集并绘制它的立方体。

现在我需要在这个模型中搜索。我想按多维数据集名称搜索并获得:

包含此名称(或该名称包含此词)的多维数据集的服务器对象类别。

我正在尝试这样:

NSPredicate * defaultPredicate = [NSPredicate predicateWithFormat:@"ANY SELF.defaultFlag = %@ AND ANY SELF.category.cube.title = %@", [NSNumber numberWithInt:1], text];

但它让我

 *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: 'multiple to-many keys not allowed here'

如何正确地做到这一点。

编辑 :

例如,如果我有:一台服务器 DemoServer whit 3 类别(Category1,Category2,Category3)并且这些类别各有 3 个立方体(Test,cube11,cube12),(cube21,cube22,cube23),(Test,Cube32,Test)

我搜索:我想得到的测试(多维数据集标题)

DemoServer whit 2 个类别(Category1,Category3),那些会有多维数据集(Test),(Test,Test)

4

1 回答 1

0

Predicates with nested to-many relationships are a bit complicated. The "obvious" predicate

[NSPredicate predicateWithFormat:@"ANY category.cube.title = %@", text]

does not work if both "category" and "cube" are to-many relationships.

What you need is a SUBQUERY:

[NSPredicate predicateWithFormat:@"SUBQUERY(category, $cat, ANY $cat.cube.title = %@).@count > 0", text]

Combined with your other predicate:

[NSPredicate predicateWithFormat:@"defaultFlag = %@ AND SUBQUERY(category, $cat, ANY $cat.cube.title = %@).@count > 0",
        [NSNumber numberWithInt:1], text]
于 2013-10-10T12:18:13.710 回答