mattsven
mattsven

Reputation: 23253

NSFetchedResultsController not resorting content changes

I'm trying to figure out why my NSFetchedResultsController instance seems to be ignoring it's sortDescriptors when content changes. All delegate methods, etc., are called, but when the relevant property that I'm sorting on changes, I only get an NSFetchedResultsChangeUpdate change notification, and resorting of content has not occurred:

Here's how I'm creating the NSFetchedResultsController instance:

NSManagedObjectContext *context = [UIApplication managedObjectContext];

_fetchRequest = [[NSFetchRequest alloc] initWithEntityName:NSStringFromClass([MCCDBook class])];
_fetchRequest.sortDescriptors = @[
    [NSSortDescriptor sortDescriptorWithKey:@"lastOpened" ascending:true selector:@selector(compare:)]
];
_fetchRequest.entity = [NSEntityDescription entityForName:_fetchRequest.entityName inManagedObjectContext:context];

_fetchedResultsController = [[NSFetchedResultsController alloc] initWithFetchRequest:_fetchRequest managedObjectContext:context sectionNameKeyPath:@"sectionTitle" cacheName:nil];
_fetchedResultsController.delegate = self;

Here's what the property that sorting is done on looks like:

enter image description here

And here is how I'm updating said property:

entity.lastOpened = [NSDate date];

[entity.managedObjectContext save:nil];

Any ideas? This one is puzzling me.

EDIT:

I've done some more testing, and it appears the problem is this: NSFetchedResultsController is only bothering to do any resorting on the original sortDescriptors. If I change the request's sortDescriptors or it's predicate after initialization of NSFetchedResultsController (and I am calling performFetch:), my delegate methods are called, but the fetchedObjects does not resort itself.

Upvotes: 2

Views: 79

Answers (1)

pbasdf
pbasdf

Reputation: 21536

As per comments, it seems that the FRC gets confused if you vary the fetch request's predicate or sort descriptors on the fly. If you need to vary them, then instantiate a completely new FRC with the new predicate and sort descriptors.

Upvotes: 2

Related Questions