NSFetchedResultsController custom sort not getting called

后端 未结 3 1665
长情又很酷
长情又很酷 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:39

    There are a couple of things I can think of. First, though this may not be your problem, you cannot sort on transient properties. But more likely is that when sorting in a model backed by a SQL store, the comparator gets "compiled" to a SQL query, and not all Objective-C functions are available. In this case, you'd need to sort in memory after the fetch is performed.

    EDIT: See this doc, specifically the Fetch Predicates and Sort Descriptors section.

    0 讨论(0)
  • 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 <NSFetchedResultsSectionInfo> 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];
        }
    }
    
    0 讨论(0)
  • 2020-12-05 21:46

    Sorry, but did you miss the final fetch part to your code snippet?:

    NSError *error;
    BOOL success = [aFetchedResultsController performFetch:&error];
    

    Don't forget to release the request too:

    [fetchRequest release];
    
    0 讨论(0)
提交回复
热议问题