我正在使用 Core Data 并与以下实体有一对多关系:
Athlete(evals)<-->>Eval(whosEval)
它以列出数据库中所有运动员的表视图开始。然后,当您选择运动员时,它会在表格视图中拉出他们的评估。问题是我这样做的方式是通过检查他们的全名。不幸的是,有可能有 2 名运动员有相同的名字。出于这个原因,我也检查了他们父母的名字,但我认为我做错了。谁能解释为什么以下方法不起作用以及我应该如何正确地做到这一点?这段代码会发生什么,如果 2 名运动员有相同的名字,他们将分享结果。即使他们父母的名字不同。
AppDelegate *appDelegate = [[UIApplication sharedApplication] delegate];
_managedObjectContext = [appDelegate managedObjectContext];
NSFetchRequest *request = [[NSFetchRequest alloc] init];
NSFetchRequest *athleteRequest = [[NSFetchRequest alloc] init];
[athleteRequest setEntity:[NSEntityDescription entityForName:@"Athlete" inManagedObjectContext:_managedObjectContext]];
NSError *athleteError = nil;
NSPredicate *athletePredicate = [NSPredicate predicateWithFormat:@"full == %@", _athletesFullName];
[athleteRequest setPredicate:athletePredicate];
NSArray *results = [_managedObjectContext executeFetchRequest:athleteRequest error:&athleteError];
if([results count] >1){
NSPredicate *athletePredicate = [NSPredicate predicateWithFormat:@"pfull == %@", _athletesParentsFullName];
[athleteRequest setPredicate:athletePredicate];
}
Athlete *athleteSelected;
if([results count] >0){
Athlete *currentAthlete = [results objectAtIndex:0];
athleteSelected = currentAthlete;
}
NSPredicate *predicate = [NSPredicate predicateWithFormat:@"whosEval == %@", athleteSelected];
[request setPredicate:predicate];
NSEntityDescription *eval = [NSEntityDescription entityForName:@"Eval" inManagedObjectContext:_managedObjectContext];
[request setEntity:eval];