我使用NSFetchedResultsController
在表格中显示数据。数据存储包含许多具有groupID
的行,并且多个实体可以共享groupID
。
每个实体也都有插入日期属性。
我想获得一个由插入日期属性排序的不同groupID
列表。以下代码有效,但它不按插入日期属性排序。我假设由于该属性不是被提取的属性之一,因此无法进行排序。
我也在使用MagicalRecord,fwiw。
有没有办法使用实体的其他属性进行排序,但没有将它们作为结果的一部分?将插入日期添加到提取的属性会使多余的多余,因为日期都是唯一的。
NSPredicate *predicate = [NSPredicate predicateWithFormat:@"userID like[c] %@", THEUSERUSERID];
NSFetchRequest *fetchRequest = [AnEntity MR_requestAllWithPredicate:predicate];
[fetchRequest setReturnsDistinctResults:YES];
[fetchRequest setResultType:NSDictionaryResultType];
[fetchRequest setPropertiesToFetch:@[@"groupID"]];
NSSortDescriptor *sortDescriptor = [[NSSortDescriptor alloc] initWithKey:@"inDate" ascending:NO];
[fetchRequest setSortDescriptors:@[sortDescriptor]];
_frController = [[NSFetchedResultsController alloc] initWithFetchRequest:fetchRequest managedObjectContext:[NSManagedObjectContext MR_rootSavingContext] sectionNameKeyPath:nil cacheName:nil];
答案 0 :(得分:1)
Chadbag ......我遇到了同样的问题。我研究了setPropertiesToFetch,发现它需要一个NSPropertyDescriptions的NSArray ...而不仅仅是属性名称。
我想如果你删除它:
[fetchRequest setPropertiesToFetch:@[@"groupID"]];
并添加:
NSDictionary *entityProperties = [entity propertiesByName];
NSPropertyDescription *propDescription = [entityProperties objectForKey:@"groupID"];
NSArray *propArray = [NSArray arrayWithObject:propDescription];
[fetchRequest setPropertiesToFetch:propArray];
这应该对你有用......它为我做了!