Note that there are some explanatory texts on larger screens.

plurals
  1. POiPhone how to use NSPredicate to filter Core Data by the parent entity?
    primarykey
    data
    text
    <p>My core data is defined as this: user has many events; event has a single user relationship;</p> <p>Both user and event are core data entities. The user entity is passed in through a storyboard segue. </p> <p>I'm trying to configure NSPredicate to populate the detail UITableView for that user with only events for that particular user. </p> <p>So far I have tried</p> <pre><code>//does not work NSPredicate* onlyThisUserPredicate = [NSPredicate predicateWithFormat:@"user == %@",self.appUser]; //does not work NSPredicate* onlyThisUserPredicate = [NSPredicate predicateWithFormat:@"SELF.user == %@",self.appUser]; </code></pre> <p><strong>What is the proper syntax to compare events and only return those that have user object equal to the specified user object?</strong></p> <p>UPDATE:</p> <p>I'm trying to be able to add events to the user with this kind of fetched results controller:</p> <pre><code>-(NSFetchedResultsController*)fetchedResultsController { if (__fetchedResultsController != nil) { return __fetchedResultsController; } // Set up the fetched results controller. // Create the fetch request for the entity. NSFetchRequest *fetchRequest = [[NSFetchRequest alloc] init]; // Edit the entity name as appropriate. NSEntityDescription *entity = [NSEntityDescription entityForName:@"Event" inManagedObjectContext:[Event managedObjectContext]]; [fetchRequest setEntity:entity]; // Set the batch size to a suitable number. [fetchRequest setFetchBatchSize:20]; //I need to configure this user NSPredicate* onlyThisUserPredicate = [NSPredicate predicateWithFormat:@"user = %@",self.appUser]; // The first sort key must match the section name key path key if present, otherwise the initial dataset would be messed up: rows in incorrect sections NSString* firstSortKey = @"createDate"; NSSortDescriptor *firstSortDescriptor = [[NSSortDescriptor alloc] initWithKey:firstSortKey ascending:YES]; NSArray *sortDescriptors = [NSArray arrayWithObjects:firstSortDescriptor, nil]; [fetchRequest setSortDescriptors:sortDescriptors]; [fetchRequest setPredicate:onlyThisUserPredicate]; // Edit the section name key path and cache name if appropriate. // nil for section name key path means "no sections". NSFetchedResultsController *aFetchedResultsController = [[NSFetchedResultsController alloc] initWithFetchRequest:fetchRequest managedObjectContext:[Event managedObjectContext] sectionNameKeyPath:nil cacheName:@"Events"]; self.fetchedResultsController = aFetchedResultsController; aFetchedResultsController.delegate = self; // [aFetchedResultsController release]; [sortDescriptors release]; [fetchRequest release]; NSError *error = nil; if (![__fetchedResultsController performFetch:&amp;error]) { /* Replace this implementation with code to handle the error appropriately. abort() causes the application to generate a crash log and terminate. You should not use this function in a shipping application, although it may be useful during development. */ NSLog(@"Unresolved error %@, %@", error, [error userInfo]); // abort(); } return __fetchedResultsController; } </code></pre> <p>Thank you!</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.
    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