user1258240
user1258240

Reputation: 855

NSFetchedResultsController doesn't sort

I have an SQL based core data managed document, and I present its contents using a tableview backed by an NSFetchedResultsController. The data is indeed shown and it's all there, but it is not sorted even though I really tried to make it sort. Here's how I set the fetched results controller:

- (void)setupFetchedResultsController // attaches an NSFetchRequest to this UITableViewController
{

    NSFetchRequest *request = [NSFetchRequest fetchRequestWithEntityName:@"Notebook"];
    NSSortDescriptor *descriptor=[[NSSortDescriptor alloc] initWithKey:@"name" ascending:YES];
    request.sortDescriptors = [NSArray arrayWithObject:descriptor];
    // no predicate because we want ALL the notebooks

    self.fetchedResultsController = [[NSFetchedResultsController alloc] initWithFetchRequest:request
                                                                    managedObjectContext:self.dbContext
                                                                      sectionNameKeyPath:nil
                                                                               cacheName:nil];

}

name is an attribute of type string. I also tried with another attribute of type NSDate. None of them work - the data is not sorted even when the tableview is presented immediately when the app starts before any changes were applied to the document.

Ideas?

Upvotes: 0

Views: 228

Answers (3)

user1258240
user1258240

Reputation: 855

Ok, I guess this one goes into the X-files section of stack overflow. The problem was resolved, and I have no idea what I did to solve it, if at all. I was working on other parts of my app for the past few days, and at some points the results of the fetched results controller started being sorted again. Perhaps it was some bug in Xcode that resolved when I restarted it, or something that I change in another part of the app, I just don't know. Thanks for the attempts at helping me though...

Upvotes: 0

Unsure1
Unsure1

Reputation: 101

Try moving the performFetch to the end of the method you quoted. The successful methods I'm using have the setup and call to perform fetch in one method, rather than separated.

Upvotes: 0

pasql
pasql

Reputation: 3825

Init your *descriptor as the following (note the "selector:" stuff )

    NSSortDescriptor *descriptor=[[NSSortDescriptor alloc] initWithKey:@"name" ascending:YES selector:@selector(localizedCaseInsensitiveCompare:)];

Hope that helps!

Upvotes: 1

Related Questions