NSPredicate to-many Relations

How can I find all categories that have subcategories with more than 1 item? I use the following code, but I get "a few to many keys that are not allowed here."

Do I need to use SUBQUERY?

NSFetchRequest *request = [NSFetchRequest fetchRequestWithEntityName:@"Categories"]; NSPredicate *predicate = [NSPredicate predicateWithFormat:@"ANY subcategories.items > 0"]; [request setPredicate:predicate]; 

Help!:)

+6
source share
3 answers

Based on my experience, you should change the predicate format string to

 NSPredicate *predicate = [NSPredicate predicateWithFormat:@"ANY subcategories.items > 0"]; 

from

 @"ANY subcategories.items > 0" 

in

 @" subcategories.items.@count > 0" 

Note that the ANY keyword must also go, or you will incorrectly request a property.

+9
source

In your NSPredicate you should use subcategories.items.@count >0 .

Sorr, this will only work if the subcategory categories have one, one.

In fact, what you should do is remove all subcategories that have no items. I think so.

Than tou should simply check if there are any subcategories, and this will mean that they are not empty. subcategories.@count >0

+3
source

You can only use one-to-many relationships in the SQL backend. This will be done, but you must add a β€œbacklink” to the ordered set (to preserve order if you use sorting - and uniqueness, because you can get several subcategories referring to the same category). Not perfect, but not bad considering the limitations.

 // Search for all subcategories with item count > 0 NSFetchRequest *request = [NSFetchRequest fetchRequestWithEntityName:@"Subcategory"]; request.predicate = [NSPredicate predicateWithFormat:@" items.@count > 0"]; NSError *error = nil; // They should have an inverse relationship to their category... // We have to iterate and insert them in to a set, but there is no additional // disk access. NSMutableOrderedSet *results = [NSMutableOrderedSet orderedSet]; [[self.projectDatabase.managedObjectContext executeFetchRequest:request error:&error] enumerateObjectsUsingBlock:^(Subcategory *obj, NSUInteger idx, BOOL *stop) { [results addObject:obj.category]; }]; 

EDIT

Unfortunately, I do not think this is possible in the SQL repository. First, let me emphasize that I’m nowhere close to the SQL expert, and I usually try to build my CoreData repositories so that these types of queries are not needed. However, the next SUBQUERY works with the InMemory repository, so it has a somewhat properly formatted subquery.

 fetchRequest.predicate = [NSPredicate predicateWithFormat:@"SUBQUERY(subcategories, $s, $s.items.@count > 0) .@count > 0"]; 

However, this predicate gives an error when using SQL repository. The docs say that there are many limitations when using predicates with SQL databases, so I'm not surprised and not inclined to spend too much time learning this.

Keypath containing the KVC aggregate, where it should not be; failed to process $ s.items. @count

+2
source

Source: https://habr.com/ru/post/912840/


All Articles