choise
choise

Reputation: 25244

mapping a subset of core-data entities as a section-key-path

I have a one-to-many relationship between:

Question<<---->Section

A section has many questions. A question has one section.

I now create a NSFetchedResultsController to get all questions seperated by its sections into UITableView-Sections.

NSFetchRequest *fetchRequest = [NSFetchRequest fetchRequestWithEntityName:@"MJUQuestion"];
NSSortDescriptor *sortByName = [NSSortDescriptor sortDescriptorWithKey:@"title" ascending:YES];
[fetchRequest setSortDescriptors:[NSArray arrayWithObject:sortByName]];

NSFetchedResultsController *aFetchedResultsController = [[NSFetchedResultsController alloc] 
    initWithFetchRequest:fetchRequest 
    managedObjectContext:context
    sectionNameKeyPath:@"section.title" 
    cacheName:nil];

Now all questions get seperated into their corresponding sections which works but is not exactly what i want.

I don't want the questions of all sections, but i want all the questions of a specific subset of sections.

Sections are themselve seperated into categorys:

Section<<----->Category

And i only want the questions of sections that belong to a specific category. if i would do a NSFetchedResultsController for Sections of that category i would add a predicate like

NSPredicate *predicate = [NSPredicate predicateWithFormat:@"category == %@", category];
[fetchRequest setPredicate:projectPredicate];

But since this is not the case and i want them as section seperator, i'm not sure how to modify my NSFetchRequest properly.

So how do i need to modify my NSFetchRequest that i get the questions of a specific subset of sections and these sections as a UITableView-Section seperator?

Thanks in advance.

Upvotes: 1

Views: 168

Answers (1)

Marcus S. Zarra
Marcus S. Zarra

Reputation: 46718

You are almost there. You need to update your predicate and use a key path:

NSPredicate *predicate = [NSPredicate predicateWithFormat:@"section.category IN %@", categoryArray];

Which will cause the request to check each question and see if its section's category is in the array. This will filter you down to a subset of categories.

Upvotes: 2

Related Questions