Reputation: 5089
Lets say i've got 2 entities -- Athlete:with "fullname" attribute -- Eval:with "pullups" attribute -- i would want to set multiple evals for each name, but how to I tell it which name i would like to add the specific evals to?
My app is quite similar to the contacts app that comes preinstalled with iOS. The root view controller has my first entity, Athlete. it is a table view that displays all athletes. When you select an athlete, it shows all his/her info, and there is a button called show eval. Eval is my second entity. Athlete has a to-many relationship with Eval, meaning one athlete can have multiple evals, but evals can only have one athlete. I know how to add evals, but when I add them, they are just there, they don't have an owner, so regardless of which athlete I select, it just shows all the evals. How do I create an eval that correlates with the Athlete that was selected?
allevals.h
//the athlete selected pushes a tableviewcontroller with all evals, but I want it to only show evals for the athlete selected.
-(void)viewWillAppear:(BOOL)animated{
self.title = [NSString stringWithFormat:@"%@'s Evaluations",_athletesFullName];
AppDelegate *appDelegate = [[UIApplication sharedApplication] delegate];
_managedObjectContext = [appDelegate managedObjectContext];
NSFetchRequest *request = [[NSFetchRequest alloc] init];
NSEntityDescription *eval = [NSEntityDescription entityForName:@"Eval" inManagedObjectContext:_managedObjectContext];
[request setEntity:eval];
NSSortDescriptor *sortDescriptor =
[[NSSortDescriptor alloc] initWithKey:@"date_recorded"
ascending:NO
selector:@selector(localizedCaseInsensitiveCompare:)];
NSArray *sortDescriptors = [[NSArray alloc]initWithObjects:sortDescriptor, nil];
[request setSortDescriptors:sortDescriptors];
NSError *error = nil;
NSMutableArray *mutableFetchResults = [[_managedObjectContext executeFetchRequest:request error:&error] mutableCopy];
if (mutableFetchResults == nil){
//handle error
}
[self setEvalArray:mutableFetchResults];
[self.tableView reloadData];
}
The view controllers are as follows, all athletes tableview is root tableviewcontroller, then there is an add barbuttonitem which allows you to add an athlete. It reloads the view, then on the tableview you have your newly added athlete, you click on him, then it shows details, plus a button to look at his evals. From there, it is another tableview of evals with a barbuttonitem for adding. Problem is, I don't know how to tell the eval that was just added who its owner is.
Upvotes: 0
Views: 75
Reputation: 4729
When adding a new eval:
create new eval entity and set the entity attributes as needed.
select the athlete that goes with the eval and call addEval:
that will link the two together.
When trying to get all of the evals for an athlete use athlete.evals
and it will just give you the eval entities attached to the athlete in question.
Upvotes: 1
Reputation: 119021
When the user selects to view the evals for an athlete you need to pass that athlete to the evals view controller. The view controller can now use it both in the filter predicate when it is getting the evals to display and to configure the link between the objects when a new eval is created.
newEval.whoTook = self.currentAthlete;
Upvotes: 0
Reputation: 150565
Your fetch request has a sot order and an entity description, but you've missed the third item it can work with.
If you want to filter only the evals that apply to an athlete, you need to set a predicate on the fetchrequest.
Upvotes: 1