NSFetchedResultsController 自定义排序未被调用 [英] NSFetchedResultsController custom sort not getting called

查看:17
本文介绍了NSFetchedResultsController 自定义排序未被调用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我目前正在尝试使用 NSFetchedResultsController 从核心数据填充我的项目中的 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 的对象,它就会进入比较器块并正确地对表格进行排序.

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: 中执行提取,但似乎我提取多少次并不重要,但什么时候.出于某种原因,它仅在对象模型更改后使用我的排序.

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,特别是提取谓词和排序描述符部分.

See this doc, specifically the Fetch Predicates and Sort Descriptors section.

这篇关于NSFetchedResultsController 自定义排序未被调用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆