How to sort Core Data fetched properties

前端 未结 8 2068
太阳男子
太阳男子 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 16:04

    Using Tim Shadel's great answer I added per-NSManagedObject subclass sorting...

    ...in Tier.m (which is a NSManagedObject subclass)...

    + (void)initialize
    {
        if(self == [Tier class])
        {
            NSFetchedPropertyDescription *displayLessonPropertyDescription = [[[Tier entityDescription] propertiesByName] objectForKey:@"displayLesson"];
            NSFetchRequest *fetchRequest = [displayLessonPropertyDescription fetchRequest];
    
            NSSortDescriptor *sortByName = [[NSSortDescriptor alloc] initWithKey:@"displayOrder" ascending:YES];
           [fetchRequest setSortDescriptors:[NSArray arrayWithObject:sortByName]];
            [sortByName release];
        }
    }
    

提交回复
热议问题