NSFetchedResultsController not sorting on initial fetch

I am having a Core Data problem with NSFetchedResultsController . I have a one to many relationship between a parent and child entity. The array in the childFetchedResults.fetchedObjects property is NOT sorted by number (number is an int32 property in the model). It doesn't seem to matter if I use the MagicalRecord convenience category methods or not.

NSFetchRequest *req = [Child MR_requestAllSortedBy:@"number" ascending:YES withPredicate:[NSPredicate predicateWithFormat:@"parent = %@", self.parent]];
childFetchedResults = [[NSFetchedResultsController alloc] initWithFetchRequest:req managedObjectContext:[NSManagedObjectContext MR_defaultContext] sectionNameKeyPath:nil cacheName:nil];
childFetchedResults.delegate = self;
NSError *error;
[childFetchedResults performFetch:&error];
NSLog(@"fetched objects: %@", childFetchedResults.fetchedObjects);

However, if I sort the array of fetched objects using the exact same sort descriptor, it works fine:

NSLog(@"fetched objects: %@", [childFetchedResults.fetchedObjects sortedArrayUsingDescriptors:[NSArray arrayWithObject:[NSSortDescriptor sortDescriptorWithKey:@"number" ascending:YES]]]);

I gather you can only use comparators which Core Data can pass on to the SQLite store when specifying sort descriptors for a fetch request. But I feel like that shouldn't matter in this case since sorting by a number has got to be supported by SQLite.

Anyone solved this? I feel like it's a similar issue to the one described here: NSSortDescriptor not being called.

As for MR_requestAllSortedBy , it's in MagicalRecord , here is the implementation:

+ (NSFetchRequest *) MR_requestAllSortedBy:(NSString *)sortTerm ascending:(BOOL)ascending inContext:(NSManagedObjectContext *)context
{
    NSFetchRequest *request = [self MR_requestAllInContext:context];

    NSSortDescriptor *sortBy = [[NSSortDescriptor alloc] initWithKey:sortTerm ascending:ascending];
    [request setSortDescriptors:[NSArray arrayWithObject:sortBy]];

    return request;
}

So this was caused by fetching against a nested MOC with unsaved changes. Either fetching with the parent MOC or saving the nested MOC prior to executing the fetch resolves the problem. Similar to what was going on in this question: NSSortdescriptor ineffective on fetch result from NSManagedContext


    NSFetchRequest *fetchRequest = [[NSFetchRequest alloc]init];
    NSEntityDescription *entity = [NSEntityDescription entityForName:@"RemainderDataBase" inManagedObjectContext:[self managedObjectContext]];
    NSSortDescriptor *nameSort = [[NSSortDescriptor alloc]initWithKey:@"title" ascending:YES];
    NSArray *sortDescriptor = [[NSArray alloc]initWithObjects:nameSort, nil];
    fetchRequest.sortDescriptors = sortDescriptor;
    NSPredicate *p1=[NSPredicate predicateWithFormat:@"startdate > %@", [NSDate date]];
    [fetchRequest setPredicate:p1];
    [fetchRequest setEntity:entity];

我认为你正在寻找上面的代码..

链接地址: http://www.djcxy.com/p/36144.html

上一篇: 如何使用Coredata与NSFetchedResultsController的有序关系

下一篇: NSFetchedResultsController在初始获取时不排序