在NSFetchedResultsController的谓词中获取属性

时间:2011-06-23 05:12:23

标签: iphone core-data predicates

我有一个带有.localConcerts获取属性的Artist对象(基本上是完整。concerts集的子集),我可以在NSFetchedResultsController谓词中使用该属性吗?

以下是我正在尝试的内容:

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

NSEntityDescription *entity = [NSEntityDescription entityForName:@"Artist" inManagedObjectContext:context];
[request setEntity:entity];

NSPredicate *predicate = [NSPredicate predicateWithFormat:@"localConcerts.@count > 0"];
[request setPredicate:predicate];

fetchedResultsController = [[NSFetchedResultsController alloc]
                            initWithFetchRequest:request
                            managedObjectContext:context
                            sectionNameKeyPath:nil
                            cacheName:nil];

但我得到了:

'keypath localConcerts not found in entity <NSSQLEntity Artist id=1>'

我是否遗漏了任何内容,或者是否无法在谓词中使用获取的属性?

1 个答案:

答案 0 :(得分:6)

显然NSPredicate只能使用数据库结构中的属性进行过滤(这很有意义)。在我的例子中,使用子查询做了诀窍:

NSPredicate *predicate = [NSPredicate predicateWithFormat:@"SUBQUERY(shows, $show, $show.distance < %@).@count > 0", [SWDataManager sharedManager].localFilterDistance];

我不知道我们能做subqueries in NSPredicate,这真是太棒了。积分转至@kyleve

相关问题