Saturday, 7 September 2013

NSFetchedResultsController Query

NSFetchedResultsController Query

Having a problem with a one to many data model and fetchedResultsController.
i have a core data model RecordDate and RecordWorkout.
RecordDate has a one to many relationship with RecordWorkout.
My view controller consists of a tableView at the bottom half.
and a date label at the top.
I have two buttons: -previousDayButtonPressed -NextDayButtonPressed
The date label holds the current date in string format and by pressing the
buttons you can scroll through next and previous days.
What i am after is for the user to flick through the days, as it flicks
through the days the app then compares the dateLabel text to the Entity
RecordDate and checks if there are any values stored of the same date.
if that is true: then to display the corresponding data from the
RecordWorkout in the table.
I have managed to do most of the code. I can fetch the dates. and i can
fetch the workouts. But the problem is inbetween fetching the dates and
workouts.
As once ive fetched the date...i need to set it to recordDate...to then be
used in the fetch for the recordWorkout.
My code is below...it will probably make more sense than the above
description:
- (id)initWithRecordDate:(RecordDate *)recordDate{
//self = [super initWithStyle:UITableViewStylePlain];
if (self)
{
self.recordDate = recordDate;
}
return self;
}
-(void)fetchRecordDates
{
NSFetchRequest *fetchRequest = [NSFetchRequest
fetchRequestWithEntityName:@"RecordDate"];
NSPredicate *predicate = [NSPredicate predicateWithFormat:@"date =
%@", dateLabel.text];
NSSortDescriptor *sortDescriptor = [NSSortDescriptor
sortDescriptorWithKey:@"date" ascending:YES];
[fetchRequest setSortDescriptors:@[sortDescriptor]];
[fetchRequest setPredicate:predicate];
fetchRequest.fetchLimit = 1;
self.fetchedDatesController = [[NSFetchedResultsController alloc]
initWithFetchRequest:fetchRequest
managedObjectContext:self.managedObjectContext
sectionNameKeyPath:nil cacheName:nil];
NSError *error;
if (![self.fetchedDatesController performFetch:&error])
{
NSLog(@"Fetch failed: %@", error);
}
}
- (void)viewDidLoad
{
[super viewDidLoad];
self.parentViewController.view.backgroundColor = [UIColor
colorWithPatternImage:[UIImage imageNamed:@"common_bg"]];
currentDate = [NSDate date];
NSString *formatString = [NSDateFormatter
dateFormatFromTemplate:@"EdMMM" options:0 locale:[NSLocale
currentLocale]];
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat:formatString];
NSString *todayString = [dateFormatter stringFromDate:currentDate];
dateLabel.text = todayString;
[self fetchRecordDates];
//set the fetched record date to recordDate
[self fetchRecordWorkouts];
}
-(void)fetchRecordWorkouts
{
NSFetchRequest *fetchRequest = [NSFetchRequest
fetchRequestWithEntityName:@"RecordWorkout"];
NSPredicate *predicate = [NSPredicate predicateWithFormat:@"recordDate
= %@", self.recordDate];
NSSortDescriptor *sortDescriptor = [NSSortDescriptor
sortDescriptorWithKey:@"recWorkoutName" ascending:YES];
[fetchRequest setSortDescriptors:@[sortDescriptor]];
[fetchRequest setPredicate:predicate];
self.fetchedWorkoutsController = [[NSFetchedResultsController alloc]
initWithFetchRequest:fetchRequest
managedObjectContext:self.managedObjectContext
sectionNameKeyPath:nil cacheName:nil];
NSError *error;
if (![self.fetchedWorkoutsController performFetch:&error])
{
NSLog(@"Fetch failed: %@", error);
}
}
Previously i managed to display the RecordDates in a tableviewcontroller
and display the RecordWorkouts in a tableviewcontroller.
Because they were separate view controllers i was managed to set the
recordDate from the first view controller like:
- (void)tableView:(UITableView *)tableView
didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
RecordDate *recordDate = (RecordDate *)[self.fetchedResultsController
objectAtIndexPath:indexPath];
detailViewController.recordDate = recordDate;
But as I am now trying to display both in one view controller..and the
RecordDate not even in a tableview...im not sure how i can set fetched
RecordDate to recordDate to be compared in RecordWorkout Fetch
Thanks

No comments:

Post a Comment