Dan*_*son 20 sorting iphone xcode core-data fetched-property
该核心数据文件指出:
与[fetched]属性关联的获取请求可以具有排序顺序,因此可以对所获取的属性进行排序.
如何在Xcode的数据模型编辑器中为fetched属性指定排序描述符?我无法在任何地方找到相关领域.我正在为iPhone平台开发,如果这有任何区别.
如果通过图形模型编辑器无法实现这一点,那么如何在代码中修改fetched属性的获取请求以使其具有排序描述符?
Tim*_*del 35
您实际上可以获取模型获取属性并向其添加排序描述符(同样,在代码中).如果您选择其中一个带有Core Data的模板,我会在您的AppDelegate中生成的标准方法中执行此操作:
顺便说说.这会对数据模型中所有模型上的所有获取属性进行排序.您可以使用它获得花哨和适应性,但它是处理7个单独模型的最简洁方法,每个模型都获取了需要按名称排序的属性.效果很好.
/**
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;
}
Run Code Online (Sandbox Code Playgroud)
mmc*_*mmc 13
您没有在图形编辑器中指定它们(据我所知).
您可以在进行提取的代码中指定它们.
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"];
Run Code Online (Sandbox Code Playgroud)
建模工具似乎没有办法在获取请求上设置排序描述符.
在加载模型之后但在将其与持久性存储协调器关联之前,应该可以[1]找到要为其控制排序顺序的已获取属性描述,并将其获取请求替换为具有排序的获取请求设置描述符.
[1]原则上这应该有效.在实践中,我还没有这样做或测试过它.
使用Tim Shadel的好答案我添加了per-NSManagedObject子类排序...
...在Tier.m(这是一个NSManagedObject子类)...
+ (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];
}
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
39122 次 |
| 最近记录: |