NSFetchedResultsController自定义排序不被调用 [英] NSFetchedResultsController custom sort not getting called
问题描述
我目前正在尝试使用NSFetchedResultsController从Core Data填充我的项目中的UITableView。我使用一个自定义搜索比较器(虽然我也尝试过选择器,并有一个相同的问题):
I am currently trying to populate a UITableView in my project from Core Data using NSFetchedResultsController. I am using a custom search with a comparator (although I have also tried a selector and had an identical problem):
if (fetchedResultsController != nil) {
return fetchedResultsController;
}
/*
Set up the fetched results controller.
*/
// Create the fetch request for the entity.
NSFetchRequest *fetchRequest = [[NSFetchRequest alloc] init];
// Edit the entity name as appropriate.
NSEntityDescription *entity = [NSEntityDescription entityForName:@"Object" inManagedObjectContext:self.managedObjectContext];
[fetchRequest setEntity:entity];
// Set the batch size to a suitable number.
[fetchRequest setFetchBatchSize:20];
NSSortDescriptor *sortDescriptor = [[NSSortDescriptor alloc] initWithKey:@"objectName" ascending:YES comparator:^(id s1, id s2) {
NSLog(@"Comparator");
//custom compare here with print statement
}];
NSLog(@"Sort Descriptor Set");
NSArray *sortDescriptors = [[NSArray alloc] initWithObjects:sortDescriptor, nil];
NSEntityDescription *entity = [NSEntityDescription entityForName:@"Object" inManagedObjectContext:self.managedObjectContext];
[fetchRequest setEntity:entity];
[fetchRequest setSortDescriptors:sortDescriptors];
NSFetchedResultsController *aFetchedResultsController = [[NSFetchedResultsController alloc] initWithFetchRequest:fetchRequest managedObjectContext:self.managedObjectContext sectionNameKeyPath:@"firstLetterOfObject" cacheName:@"Objects"];
[aFetchedResultsController release];
[fetchRequest release];
[sortDescriptor release];
[sortDescriptors release];
if (![fetchedResultsController performFetch:&error]) {
NSLog(@"Unresolved error %@, %@", error, [error userInfo]);
abort();
}
return fetchedResultsController;
当我进入这个选项卡时,我已经记录了整个程序,发现NSFetchedResultsController甚至没有在获取时输入比较器块。
When I enter this tab, I have logged all over the program and found that the NSFetchedResultsController does not even enter the comparator block when fetching. It instead sorts it with some default sorting method.
如果我删除并添加一个带有objectName的Object,然后它会进入比较器块并正确地对表进行排序。
If I delete and add an Object with an objectName, however, it then does enter the comparator block and correctly sort the table.
为什么NSFetchedResultsController在管理对象模型更改之前不使用比较器进行排序?
Why does the NSFetchedResultsController not sort using the comparator until the managed object model is changed?
我也试过也关闭缓存,和/或执行在viewDidLoad:fetch,但似乎我获取了多少没有关系,但时间。因为某种原因,它只会在对象模型更改后使用我的排序。
Notes: I have tried also turning off caching, and/or performing a fetch in viewDidLoad:, but it seems that how many times I fetch does not matter, but when. For some reason it only uses my sorting after the object model has been changed.
推荐答案
的。首先,虽然这可能不是你的问题,你不能对瞬态属性进行排序。但更有可能的是,在由SQL存储支持的模型中排序时,比较器将编译为SQL查询,而不是所有的Objective-C函数都可用。在这种情况下,您需要在执行提取后在内存中进行排序。
There are a couple of things I can think of. First, though this may not be your problem, you cannot sort on transient properties. But more likely is that when sorting in a model backed by a SQL store, the comparator gets "compiled" to a SQL query, and not all Objective-C functions are available. In this case, you'd need to sort in memory after the fetch is performed.
编辑:请参阅此 doc ,特别是Fetch Predicates and Sort Descriptors部分。
See this doc, specifically the Fetch Predicates and Sort Descriptors section.
这篇关于NSFetchedResultsController自定义排序不被调用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!