Fetched Properties inside NSFetchedResultsController's predicate

 ̄綄美尐妖づ 提交于 2020-01-23 01:54:10

问题


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?

Here's what I'm trying:

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];

But I'm getting:

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

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


回答1:


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.



来源:https://stackoverflow.com/questions/6449615/fetched-properties-inside-nsfetchedresultscontrollers-predicate

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!