Note that there are some explanatory texts on larger screens.

plurals
  1. PONSFetchedResultsController not sorting on initial fetch
    primarykey
    data
    text
    <p>I am having a Core Data problem with <code>NSFetchedResultsController</code>. I have a one to many relationship between a parent and child entity. The array in the <code>childFetchedResults.fetchedObjects</code> property is NOT sorted by <code>number</code> (number is an int32 property in the model). It doesn't seem to matter if I use the <code>MagicalRecord</code> convenience category methods or not.</p> <pre><code>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:&amp;error]; NSLog(@"fetched objects: %@", childFetchedResults.fetchedObjects); </code></pre> <p>However, if I sort the array of fetched objects using the exact same sort descriptor, it works fine: </p> <pre><code>NSLog(@"fetched objects: %@", [childFetchedResults.fetchedObjects sortedArrayUsingDescriptors:[NSArray arrayWithObject:[NSSortDescriptor sortDescriptorWithKey:@"number" ascending:YES]]]); </code></pre> <p>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.</p> <p>Anyone solved this? I feel like it's a similar issue to the one described here: <a href="https://stackoverflow.com/questions/10109115/nssortdescriptor-not-being-called">NSSortDescriptor not being called</a>.</p> <p>As for <code>MR_requestAllSortedBy</code>, it's in <code>MagicalRecord</code>, here is the implementation:</p> <pre><code>+ (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; } </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload