Note that there are some explanatory texts on larger screens.

plurals
  1. PONSPredicate multiple ANY query with CoreData
    primarykey
    data
    text
    <p>I'm trying to find all instances of an object that contain a reference to a combination of separate objects in my object graph.</p> <pre><code>recommendation </code></pre> <p><em>may</em> contain one or more of the following three objects:</p> <pre><code>damageType areaDamaged validVehicles </code></pre> <p>This structure is built from an import of an existing system's file format and I am unable to restructure the object graph.</p> <p>I'm using an <code>NSPredicate</code> to find all <code>recommendation</code> objects that have a <code>damageType</code> matching a selected damage as follows:</p> <pre><code>NSFetchRequest *fetchRequestDamages = [NSFetchRequest fetchRequestWithEntityName:NSStringFromClass([Recommendation class])]; NSPredicate *predicate = [NSPredicate predicateWithFormat:@"ANY damageType == %@", _currentRecordedDamage.damageType]; </code></pre> <p>But want the filter to return all <code>Recommendations</code> that have matches for a specific <code>damageType</code>, <code>areaDamaged</code> and <code>validVehicle</code></p> <p>I've tried </p> <pre><code>NSMutableArray *predicates = [[NSMutableArray alloc] initWithCapacity:2]; NSPredicate *predicate = [NSPredicate predicateWithFormat:@"ANY damageType == %@", _currentRecordedDamage.damageType]; [predicates addObject:predicate]; NSPredicate *predicate2 = [NSPredicate predicateWithFormat:@"ANY areaDamaged == %@", _currentAreaDamaged]; [predicates addObject:predicate2]; NSPredicate *predicate3 = [NSPredicate predicateWithFormat:@"ANY validVehicles == %@", _currentVehicle]; [predicates addObject:predicate3]; fetchRequestDamages.predicate = [NSCompoundPredicate andPredicateWithSubpredicates:predicates]; fetchRequestDamages.sortDescriptors = @[[NSSortDescriptor sortDescriptorWithKey:@"name" ascending:YES]]; self.fetchedResultsController = [[NSFetchedResultsController alloc] initWithFetchRequest:fetchRequestDamages managedObjectContext:[RKManagedObjectStore defaultStore].mainQueueManagedObjectContext sectionNameKeyPath:nil cacheName:nil]; self.fetchedResultsController.delegate = self; NSError *error; [self.fetchedResultsController performFetch:&amp;error]; int resultsFound = self.fetchedResultsController.fetchedObjects.count; </code></pre> <p>but it seems this returns the set of all objects satisfying any of the predicates - I'd like the set of objects that match all three.</p> <p>I'm looking into using <code>SUBQUERY</code> but can't quite make sense of how to create this query?</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload