Note that there are some explanatory texts on larger screens.

plurals
  1. POFiltering with NSPredicate in one-to-many relationships
    primarykey
    data
    text
    <p>I tried a lot of the solutions in stackoverflow but I'm not able to find a valid one. I have a core data model with two entities: Client and Destination. Both are wrapped by <code>NSManagedObject</code>subclasses.</p> <p>Client has some properties and a one-to-many relationship called destinations. Destination has a property called default_dest that is wrapped by a <code>NSNumber</code> and an inverse relationship called client.</p> <p>I have a <code>UITableViewController</code> where I'm using the following <code>fetchedController</code> property. The request works well. I'm able to retrieve clients stored in SQLite.</p> <pre class="lang-m prettyprint-override"><code>if (fetchedResultsController) return fetchedResultsController; NSFetchRequest *fetchRequest = [[NSFetchRequest alloc] init]; NSEntityDescription *entity = [NSEntityDescription entityForName:@"Client" inManagedObjectContext:self.managedObjectContext]; [fetchRequest setEntity:entity]; [fetchRequest setFetchBatchSize:20]; NSSortDescriptor *sortDescriptor = [[NSSortDescriptor alloc] initWithKey:@"code" ascending:YES]; NSArray *sortDescriptors = [[NSArray alloc] initWithObjects:sortDescriptor, nil]; [fetchRequest setSortDescriptors:sortDescriptors]; fetchedResultsController = [[NSFetchedResultsController alloc] initWithFetchRequest:fetchRequest managedObjectContext:self.managedObjectContext sectionNameKeyPath:nil cacheName:nil]; fetchedResultsController.delegate = self; [fetchRequest release]; [sortDescriptor release]; [sortDescriptors release]; return fetchedResultsController; </code></pre> <p>I would make another step. I would now filter the destinations retrieved from the previous request (that are contained in destinations <code>NSSet</code>) for each client. In particular, the destination can be added only if its default_dest value is 1.</p> <p>To fix this specification I tried to add an <code>NSPredicate</code> like the following:</p> <pre class="lang-m prettyprint-override"><code>NSPredicate* predicate = [NSpredicate predicateWithFormat:@"ANY destinations.default_dest == %@", [NSNumber numberWithInt:1]]; </code></pre> <p>Then I set it in fetchRequest as:</p> <pre class="lang-m prettyprint-override"><code>[fetchRequest setPredicate:predicate]; </code></pre> <p>Each time I run the request, it returns a "to-many-relationship fault destinations...". What does it mean?</p> <p>I've read <a href="https://stackoverflow.com/questions/3123950/iphone-core-data-relationship-fault">iphone-core-data-relationship-fault</a> but I don't understand what does it mean.</p> <p>So, my questions are: Is it possible to reach a similar goal? If yes, do you have any suggestions?</p> <p><strong>Notes</strong></p> <p>Obviously I could iterate over destinations set but I don't know if could be an expensive iteration and how many records there are.</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