How to sort Core Data fetched properties

前端 未结 8 2027
太阳男子
太阳男子 2020-12-12 15:30

The Core Data Documentation states that:

The fetch request associated with the [fetched] property can have a sort ordering, and thus the fetched prope

8条回答
  •  囚心锁ツ
    2020-12-12 15:54

    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"];
    

提交回复
热议问题