NSFetchedResultsController custom sort not getting called

后端 未结 3 1666
长情又很酷
长情又很酷 2020-12-05 21:08

I am currently trying to populate a UITableView in my project from Core Data using NSFetchedResultsController. I am using a custom search with a comparator (although I have

3条回答
  •  野趣味
    野趣味 (楼主)
    2020-12-05 21:41

    I see the same problem and a way to work around it is to modify an object, save the change then restore it to its original value and save again.

        // try to force an update for correct initial sorting bug
    NSInteger count = [self.fetchedResultsController.sections count];
    if (count > 0) {
        id  sectionInfo = [[self.fetchedResultsController sections] objectAtIndex:0];
        count = [sectionInfo numberOfObjects];
        if (count > 0) {
            NSManagedObject *obj = [self.fetchedResultsController objectAtIndexPath:[NSIndexPath indexPathForRow:0 inSection:0]];
            NSString *name = [obj valueForKey:@"name"];
            [obj setValue:@"X" forKey:@"name"];
            // Save the context.
            [self saveContext];
            [obj setValue:name forKey:@"name"];
            // Save the context.
            [self saveContext];
        }
    }
    

提交回复
热议问题