I'm running into a problem which should be very basic and have an obvious answer, and I have read through some of Apple's documentation as well as answers here and tutorials online.
I have a tableview displaying a list of "Month" entities (app uses Core Data). It segues to another tableview that I want to show a list of all days contained in that parent month. So, tapping a row on the list of "Month" entities would ideally list only the days in, say, January.
Both the Month and Days tableviews have a separate segue to a regular view controller (not a tableview VC) which adds Month entries and Day entries, respectively.
The data is modeled so Month has a to-many relationship (daysInMonths) with Days as its target, while the inverse is a to-one relationship (parentMonth).
Month attributes are 'month' and 'year'. Days attributes are 'month', 'day', and 'year'.
All the attributes on the model objects are currently strings (not ideal, but I'll change this in the next exercise).
I can add Month and Days entries. I can delete them. The thing is, when I tap on a row in the first tableview (Month), it displays every day entry, not filtered at all.
I'm assuming that when I set up the fetched results controller in my Days tableview .m file, I would use a predicate to filter only the days that match a given parent month.
My fetch request in this tableview is on a Days object. Would I have to instantiate on Month object within that fetch method when setting the predicate?
I don't want to create an additional Model object that shows up in the tableview when all I want to do is set the "parentMonth' relationship to associate the data correctly.
I understand the idea of relationships conceptually, but I'm a bit unclear how to actually set them in code.
What is the best place to set up such a filter?
I realize I'm probably not being clear enough, so I'll answer any other questions as best I can.
I know I'm missing something basic, as this kind of functionality is used everywhere, but reading on my own hasn't made the point hit home yet...
@implementation DaysCDTVC
- (void)setupFetchedResultsController
{
// 1 - Get Entity
NSString *entityName = @"Days";
NSLog(@"Setting up a Fetched Results Controller for the Entity named %@", entityName);
// 2 - Request
NSFetchRequest *request = [NSFetchRequest fetchRequestWithEntityName:entityName];
// 3 - Filter
//request.predicate = [NSPredicate predicateWithFormat: ????????????????
// 4 - Sort
request.sortDescriptors = [NSArray arrayWithObject:[NSSortDescriptor sortDescriptorWithKey:@"month"
ascending:YES
selector:@selector(localizedCaseInsensitiveCompare:)]];
//5 - Fetch
self.fetchedResultsController = [[NSFetchedResultsController alloc] initWithFetchRequest:request
managedObjectContext:self.managedObjectContext
sectionNameKeyPath:nil
cacheName:nil];
[self performFetch];
}
the "performFetch" method is in an associated class. It's basically the same as what Paul Hegarty taught in the Stanford iTunes lectures.