Scenario :
I have an expense tracking iOS Application and I am storing expenses from a expense detail view controller into a table view that shows the list of expenses along with the category and amount.
On the top of the tableview, is a UIView with CALENDAR button, a UILabel text showing the date (for example: Oct 23, 2012 (Sun)) and 2 more buttons on the side.
The pressing of the calendar button opens up a custom calendar with the current date and the two buttons are for decrementing and incrementing the date correspondingly.
I want to save the expenses according to the date which is an attribute in my Core data entity “Expense”.
Question: Suppose I press the calendar button and choose some random date from there, the table view underneath it, should show that day’s particular expenses. What I mean is I want the table view to just show a particular date’s expenses and if I press the button for incrementing the date or decrementing the date, the table view should show that day’s expenses. I am using NSFetchedResultsController and Core Data in order to save my expenses.
Any thoughts on how I would achieve this? Here’s the code for FRC.
-(NSFetchedResultsController *)fetchedResultsController
{
if(_fetchedResultsController != nil)
{
return _fetchedResultsController;
}
AppDelegate * applicationDelegate = (AppDelegate *) [[UIApplication sharedApplication] delegate];
NSManagedObjectContext * context = [applicationDelegate managedObjectContext];
NSFetchRequest * request = [[NSFetchRequest alloc]init];
[request setEntity:[NSEntityDescription entityForName:@"Money" inManagedObjectContext:context]];
NSSortDescriptor *sortDescriptor1 =
[[NSSortDescriptor alloc] initWithKey:@"rowNumber"
ascending:YES];
NSArray * descriptors = [NSArray arrayWithObjects:sortDescriptor1, nil];
[request setSortDescriptors: descriptors];
[request setResultType: NSManagedObjectResultType];
[request setIncludesSubentities:YES];
[sortDescriptor1 release];
self.fetchedResultsController = [[NSFetchedResultsController alloc] initWithFetchRequest:request
managedObjectContext:context
sectionNameKeyPath:nil
cacheName:nil];
self.fetchedResultsController.delegate = self;
[request release];
NSError *anyError = nil;
if(![_fetchedResultsController performFetch:&anyError])
{
NSLog(@"error fetching:%@", anyError);
}
return _fetchedResultsController;
}
Thanks guys.
You would have to create a new
NSFetchedResultsControllerwith a newNSFetchRequestthat has an appropriately setNSPredicate:Don’t forget to call
[self.tableView reloadData];after assigning the new FRC.Edit:
You can assign a predicate to an
NSFetchRequestwhich then is assigned to the fetchedResultsController. You can think of the predicate as a filter.If you add this to the fetch request by calling
[fetchRequest setPredicate:predicate];you tell the fetched request to only fetch results where to date property of theNSManagedObjectmatches the date you provide in the predicate. Which is exactly what you want here.So if you have a method that’s called after the user selected a date you could modify it like this:
Notice that if you’re not using ARC (which you should) you’d have to release the allocated objects appropriately.