0

「Eval(whosEval)」と呼ばれるエンティティの関係をチェックする NSPredicate があり、その関係 whosEval が別のエンティティ「Athlete(evals)」のフルネームと選択されたアスリートと一致する場合、すべての Evals が表示されます。その特定のアスリートのために。残念ながら、私の述語は奇妙なことをしています。アスリートがいない場合に、1 つ追加してから評価を追加すると、すべての評価が表示され、次に別のアスリートを追加すると、2 番目のアスリートには、そのアスリートに追加した評価が表示されず、代わりに他のアスリートのすべての評価が表示されます。 . 私は何か間違ったことをしていますか?Eval ビュー コントローラを追加する前に、すべての評価ビュー コントローラが表示されます。

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 に答える