取NSFetchedResultsController的谓词中的

取NSFetchedResultsController的谓词中的

本文介绍了获取NSFetchedResultsController的谓词中的属性的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个Artist对象与 .localConcerts fetched属性(基本上是完整的音乐会集),我可以在NSFetchedResultsController谓词中使用该属性吗?

I have an Artist object with a .localConcerts fetched property (basically a subset of the full .concerts set), can I use that property inside my NSFetchedResultsController predicate?

这里是我想要的:

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>'


$ b

Am I missing anything or is it just not possible to use fetched properties inside predicates?

推荐答案

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

Apparently NSPredicate can only filter using attributes that are in the database structure (this makes sense). In my case, using a subquery did the trick:

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

我不知道我们可以做,这是伟大的知道。信用额请前往。

I had no idea we could do subqueries in NSPredicate, that's great to know. Credits go to @kyleve.

这篇关于获取NSFetchedResultsController的谓词中的属性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-29 13:37