I'm trying to design my CoreData model for this situation:
We have Song
object which have to-many relationship with some List
.
Each List
have name
property and also have to-many relationship with Song
These many-to-many relationship is needed because one Song
can be presented in each List
at the same time.
I need to somehow obtain list of Song
s grouped by these headers (name of List
) using NSFetchedResultsController
(since user may have 10,000 songs and I don't want to store it somewhere in NSArray
).
When I try to set sectionNameKeyPath:
with some value, e.g. @"List.name"
I've got an error "to-many relationship is not allowed" and of course this makes sense.
So I need help how to design my model to be able to have Song
s and some List
s like "History" / "Next Playing" etc. and to be able to fetch all the data with NSFetchedResultsController
.
Any help appreciated. If my explanation wasn't clear, please ask additional info.
Part of my code:
- (void)initFetch {
NSFetchRequest *fetchRequest = [[NSFetchRequest alloc] initWithEntityName:@"Song"];
[fetchRequest setSortDescriptors:@[[NSSortDescriptor sortDescriptorWithKey:@"index" ascending:YES]]];
fetchRequest.predicate = [NSPredicate predicateWithFormat:@"ANY list.@count != 0"];
fetchRequest.fetchBatchSize = 20;
self.fetchedResultsController = [[NSFetchedResultsController alloc] initWithFetchRequest:fetchRequest managedObjectContext:self.context sectionNameKeyPath:@"name" cacheName:nil];
[self.fetchedResultsController setDelegate:self];
NSError *error = nil;
[self.fetchedResultsController performFetch:&error];
if (error) {
NSLog(@"Unable to perform fetch.");
NSLog(@"%@, %@", error, error.localizedDescription);
}
}