Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
395 views
in Technique[技术] by (71.8m points)

ios - Sort Descriptor Not functioning correctly or consistently

I am using a sort descriptor and a set predicate between 4 entities.

entity one: workoutType Relationship workouts 1 to many entity two: workoutSet Relationship days 1 to many entity three: workoutDay relationship exercises 1 to many entity four: workoutExercise

Inverses are also set.

The problem i am receiving is that the sortDescriptor is working correctly behind the scenes, but what is displayed is incorrect.

For example in my workoutDay entity i have attribute dayName of string type.

stored values: day 1, day 2, day 3.

when run shown values in tableview: day 1, day 3, day 2.

When i click on the days from top to bottom:

day 1....next view controller shows correct day 1 data.

day 3....next view controller shows day 2 data (Not day 3).

day 2....next view controller shows day 3 data (Not day 2).

So the sort descriptor is working but what is shown does not correlate to what is being selected.

Code:

-(void)fetchWorkoutDays
{
    NSFetchRequest *fetchRequest = [NSFetchRequest fetchRequestWithEntityName:@"WorkoutDay"];
    NSPredicate *predicate = [NSPredicate predicateWithFormat:@"workoutSet = %@", self.workoutSet];
    [fetchRequest setPredicate:predicate];
    NSSortDescriptor *sortDescriptor = [NSSortDescriptor sortDescriptorWithKey:@"dayName" ascending:YES];
    [fetchRequest setSortDescriptors:@[sortDescriptor]];
    self.fetchedResultsController = [[NSFetchedResultsController alloc]
                                 initWithFetchRequest:fetchRequest managedObjectContext:self.managedObjectContext
                                 sectionNameKeyPath:nil cacheName:nil];
    //self.fetchedResultsController.delegate = self;
    NSError *error;
    if (![self.fetchedResultsController performFetch:&error])
    {
        NSLog(@"Fetch failed: %@", error);
    }

 }


- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
 {
     static NSString *CellIdentifier = @"Cell";
     UITableViewCell *cell =
     [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
     if (cell == nil)
     {
         cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle
                                  reuseIdentifier:CellIdentifier];
         cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
     }
     WorkoutDay *day = [self.workoutSet.days.allObjects objectAtIndex:indexPath.row];

     cell.textLabel.text = day.dayName;
     return cell;
}

Thank you for any help.

If any more code is required i can show it.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

workoutSet.days is a nun-ordered collection so each time you configure a cell you're getting the items in a random order. You should be asking the FRC to return you the appropriate object for the indexPath.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...