1

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;
}
Community
  • 1
  • 1
ehope
  • 506
  • 3
  • 9
  • Provide the code for `[Child MR_requestAllSortedBy:@"number" ascending:YES withPredicate:[NSPredicate predicateWithFormat:@"parent = %@", self.parent]];`. Then, why first you sort first with two different descriptor (first by number and then by date)? Could you provide more info about your model? Thanks. – Lorenzo B May 25 '12 at 16:10
  • Sorry. it's number in both, copy paste problem. The model has two entities, Parent and Child. Parent has a one to many relationship to Child. The number attribute on the Child is just a property to preserve the display order -- the children are reorderable via tableview editing. – ehope May 25 '12 at 16:14
  • Please edit your question and insert the code there. Thanks. – Lorenzo B May 25 '12 at 16:17
  • Thank you for editing your question. What about `NSFetchRequest *request = [self MR_requestAllInContext:context];`? – Lorenzo B May 25 '12 at 16:27
  • `MR_requestAllSortedBy:ascending:withPredicate:` not same method as `MR_requestAllSortedBy:ascending:inContext:` method description? – trapper May 25 '12 at 16:31
  • This is a 'MagicalRecord' question, not really `NSFetchedResultsController` – trapper May 25 '12 at 16:43
  • Well I'll be. You are right trapper, it was an MR problem. I switched away from using the MR shorthands and everything works. Guess its time to figure out where the problem lies in MR.. – ehope May 25 '12 at 17:01
  • @ErikHope Please create an answer for your question (with solution details) and mark it as correct. Thanks. – Lorenzo B May 26 '12 at 10:46

2 Answers2

2

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

Community
  • 1
  • 1
ehope
  • 506
  • 3
  • 9
0
    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];

i think you are looking for the above code..