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
source share