奇怪的NSPredicate行为?

时间:2013-08-17 22:57:41

标签: ios objective-c cocoa-touch cocoa core-data

我有一个NSPredicate,它检查一个名为“Eval(whosEval)”的实体的关系,如果关系whosEval与另一个实体的全名匹配,“运动员(evals)”,与所选的运动员,它显示那个特定运动员的所有爆炸。不幸的是,我的谓词做的很奇怪。如果我没有运动员,然后添加一个,然后添加一个评估,它显示他们所有的逃避,然后我添加另一个,第二个运动员将不会显示我为该运动员添加的任何遗嘱,而是显示所有其他运动员的遗嘱。我做错了什么吗?在添加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];
}

1 个答案:

答案 0 :(得分: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];

}