0

我有一个 NSPredicate,它检查名为“Eval(whosEval)”的实体的关系,如果该关系 whosEval 与另一个实体的全名“Athlete(evals)”匹配,则显示所有 Eval对于那个特定的运动员。不幸的是,我的谓词正在做奇怪的事情。如果我没有运动员,然后添加一个,然后添加一个评估,它会显示他们的所有评估,然后我添加另一个,第二个运动员不会显示我为该运动员添加的任何评估,而是显示所有其他运动员的评估. 我做错了什么吗?在添加 Eval 视图控制器之前显示所有 evals 视图控制器。

allEvals.m

-(void)viewWillAppear:(BOOL)animated{

    self.title = [NSString stringWithFormat:@"%@'s Evaluations",_athletesFullName];

    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];
    Athlete *currentAthlete = [results objectAtIndex:0];

   NSPredicate *predicate = [NSPredicate predicateWithFormat:@"whosEval == %@", currentAthlete];
   [request setPredicate:predicate];
    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];

}

addEval.m

-(void)saveEval{

//insert saving attributes here
        AppDelegate *appDelegate = [[UIApplication sharedApplication] delegate];
        _managedObjectContext = [appDelegate managedObjectContext];
        NSFetchRequest *request = [[NSFetchRequest alloc] init];

        [request setEntity:[NSEntityDescription entityForName:@"Athlete" inManagedObjectContext:_managedObjectContext]];
        NSError *error = nil;
        NSPredicate *predicate = [NSPredicate predicateWithFormat:@"full == %@", _whichAthletesEval];
        [request setPredicate:predicate];
        NSArray *results = [_managedObjectContext executeFetchRequest:request error:&error];
        Athlete *currentAthlete = [results objectAtIndex:0];

         [eval setWhosEval:currentAthlete];
        NSLog(@"This Eval Belongs to: %@",eval.whosEval);
        NSLog(@"whichAthletesEval value is equal to: %@", _whichAthletesEval);
    }
    NSError *error = nil;
    if(![_managedObjectContext save:&error]){
        //handle dat error
    }
    [self.navigationController popViewControllerAnimated:YES];
}
4

1 回答 1

0

我在您的viewWillAppear:方法中留下了内联注释(以我的姓名首字母签名),指出了我认为是错误的地方。从本质上讲,您获取的是任意运动员而不是被选中的运动员(我假设它是 . 的来源_athletesFullName。就您的saveEval方法而言,您的命名约定很难遵循,因此我不清楚您还有什么问题这里。

-(void)viewWillAppear:(BOOL)animated{
    self.title = [NSString stringWithFormat:@"%@'s Evaluations",_athletesFullName];

    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;
    // No predicate is used at all for the contents of results here - see below - CV
    NSArray *results = [_managedObjectContext executeFetchRequest:athleteRequest error:&athleteError];
    NSPredicate *athletePredicate = [NSPredicate predicateWithFormat:@"full == %@", _athletesFullName];
    // This predicate never used as another predicate is set a few lines below - CV
    [request setPredicate:athletePredicate];
    // currentAthlete will not necessarily be the one conforming to athletePredicate
    // just the first in the array of every Athlete you've fetched - CV
    Athlete *currentAthlete = [results objectAtIndex:0];

    // because currentAthlete is arbitrary, you're now creating a predicate you likely didn't intend to - CV
   NSPredicate *predicate = [NSPredicate predicateWithFormat:@"whosEval == %@", currentAthlete];
   [request setPredicate:predicate];
    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];

}
于 2013-08-17T23:12:40.850 回答