Note that there are some explanatory texts on larger screens.

plurals
  1. POFetching contacts based on tag
    primarykey
    data
    text
    <p>I'm trying to <strong>modify</strong> a simple Core Data fetch request for contacts to only look for contacts with a relationship with a certain tag. Contact and Tag are both entities with a many-to-many relationship.</p> <p>I understand with Core Data I can do this by first fetching the <strong>Tag</strong> object, and then calling <strong>tag.contact</strong>, but I don't want to do it this way as the rest of the code is dependent on the fact that the fetchResultsController returns <strong>Contact</strong> objects, not <strong>Tag</strong> objects.</p> <p>If I were to do relational databasing, I could do a simple cross-table query and find all contacts with a certain tag. Is there a <strong>simple</strong> way I can replicate this via Core Data?</p> <pre><code>-(NSFetchedResultsController *) fetchedResultsController { //if fetch controller already exists if(_fetchedResultsController != nil) { return _fetchedResultsController; } //create a new fetch request NSFetchRequest *fetchRequest = [[NSFetchRequest alloc] init]; NSEntityDescription *entity = [NSEntityDescription entityForName:@"Contact" inManagedObjectContext:[self managedObjectContext]]; [fetchRequest setEntity:entity]; NSSortDescriptor *sortDescriptor = [[NSSortDescriptor alloc] initWithKey:@"lastName" ascending:YES]; NSArray *sortDescriptors = [[NSArray alloc] initWithObjects:sortDescriptor, nil]; [fetchRequest setSortDescriptors:sortDescriptors]; //instantiate the fetch controller with the fetch request and sort by last name into sections _fetchedResultsController = [[NSFetchedResultsController alloc] initWithFetchRequest:fetchRequest managedObjectContext:[self managedObjectContext] sectionNameKeyPath:nil cacheName:nil]; //declare delegate of fetch controller as self _fetchedResultsController.delegate = self; NSLog(@"fetchResultsController Created"); return _fetchedResultsController; } </code></pre>
    singulars
    1. This table or related slice is empty.
    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