Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to sort Core Data fetched properties

Tags:

The Core Data Documentation states that:

The fetch request associated with the [fetched] property can have a sort ordering, and thus the fetched property may be ordered.

How do I specify the sort descriptors for the fetched property in Xcode's data model editor? I can't find a relevant field anywhere. I'm developing for the iPhone platform, if this makes any difference.

If this is not possible via the graphical model editor, how do I go about modifying the fetch request for the fetched property in code so that it has a sort descriptor?

like image 308
Daniel Dickison Avatar asked Jul 01 '09 23:07

Daniel Dickison


People also ask

What are fetched properties Core Data?

Fetched Properties in Core Data are properties that return an array value from a predicate. A fetched property predicate is a Core Data query that evaluates to an array of results.

What class would you use to filter fetched results from Core Data?

Core Data fetch requests can use predicates in SwiftUI just like they can with UIKit, all by providing a predicate property to your @FetchRequest property wrapper. If you followed my Core Data and SwiftUI set up instructions, you've already injected your managed object context into the SwiftUI environment.

How do I use Core Data?

Use Core Data to save your application's permanent data for offline use, to cache temporary data, and to add undo functionality to your app on a single device. To sync data across multiple devices in a single iCloud account, Core Data automatically mirrors your schema to a CloudKit container.

What is Nsfetchrequest?

A description of search criteria used to retrieve data from a persistent store.


2 Answers

You can actually grab the model fetched property and add the sort descriptors to it (again, in code). I did this in the standard method that XCode generates in your AppDelegate if you choose one of the templates with Core Data:

By the way. This sorts ALL fetched properties on ALL models in your data model. You could get fancy and adaptive with it, but it was the most succinct way to handle sorting the 7 separate models that each had fetched properties that needed to be sorted by name. Works well.

/**  Returns the managed object model for the application.  If the model doesn't already exist, it is created by merging all of the models found in the application bundle.  */ - (NSManagedObjectModel *)managedObjectModel {      if (managedObjectModel != nil) {         return managedObjectModel;     }     managedObjectModel = [[NSManagedObjectModel mergedModelFromBundles:nil] retain];          // Find the fetched properties, and make them sorted...     for (NSEntityDescription *entity in [managedObjectModel entities]) {         for (NSPropertyDescription *property in [entity properties]) {             if ([property isKindOfClass:[NSFetchedPropertyDescription class]]) {                 NSFetchedPropertyDescription *fetchedProperty = (NSFetchedPropertyDescription *)property;                 NSFetchRequest *fetchRequest = [fetchedProperty fetchRequest];                  // Only sort by name if the destination entity actually has a "name" field                 if ([[[[fetchRequest entity] propertiesByName] allKeys] containsObject:@"name"]) {                     NSSortDescriptor *sortByName = [[NSSortDescriptor alloc] initWithKey:@"name" ascending:YES];                     [fetchRequest setSortDescriptors:[NSArray arrayWithObject:sortByName]];                     [sortByName release];                 }             }         }     }      return managedObjectModel; } 
like image 194
Tim Shadel Avatar answered Oct 11 '22 19:10

Tim Shadel


You don't specify them in the graphical editor (as far as I know).

You specify them in the code where you make the fetch.

NSFetchRequest* request = [[NSFetchRequest alloc] init]; NSEntityDescription* entity = [NSEntityDescription entityForName:@"whatYouAreLookingFor"     inManagedObjectContext:self.managedObjectContext]; [request setEntity:entity];  // here's where you specify the sort NSSortDescriptor* sortDescriptor = [[NSSortDescriptor alloc]                                 initWithKey:@"name" ascending:YES]; NSArray* sortDescriptors = [[[NSArray alloc] initWithObjects: sortDescriptor, nil] autorelease]; [request setSortDescriptors:sortDescriptors]; [sortDescriptor release];  fetchedResultsController = [[NSFetchedResultsController alloc]                initWithFetchRequest:request                managedObjectContext:self.managedObjectContext                  sectionNameKeyPath:nil                           cacheName:@"myCache"]; 
like image 41
mmc Avatar answered Oct 11 '22 20:10

mmc