NSFetchedResultsController与NSPredicate不更新 [英] NSFetchedResultsController with NSPredicate not updating

查看:100
本文介绍了NSFetchedResultsController与NSPredicate不更新的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我从核心数据库中获取数据,并将其显示在 UITableView 中。我使用 NSFetchedResultController 来获取数据。我希望用户能够在数据库中进行搜索,并使用 NSPredicate 做到这一点。

I fetch data from a Core Data-base and present them in a UITableView. I use a NSFetchedResultController to fetch the data. I want the user to be able to search in the database, and to do that I use a NSPredicate.

数据显示在 UITableView 中,一切正常,直到我设置了 NSPredicate NSFetchedResultController

The data is presented in the UITableView and everything works well until I set the NSPredicate to the NSFetchedResultController.

我在中使用它ViewDidLoad 方法:

    self.fetchedResultsController = nil;
fetchedResultsController_ = nil;

NSError *error = nil;
if (![[self fetchedResultsController] performFetch:&error]) {
    NSLog(@"Unresolved error %@, %@", error, [error userInfo]);
    exit(-1);
}

并且当用户从输入文本时UITextField ,该文本将转到新的 NSPredicate

and when the user has entered a text from the UITextField, that text will go to the new NSPredicate.

搜索开始:

NSPredicate *pred = nil;
pred = [NSPredicate predicateWithFormat:@"(Designation BEGINSWITH '22')"];
[fetchedResultsController_.fetchRequest setPredicate:pred];

NSError *error = nil;
if (![[self fetchedResultsController] performFetch:&error]) {
    NSLog(@"Unresolved error %@, %@", error, [error userInfo]);
    exit(-1);
}

[tView reloadData];

现在我使用 @(名称开头为'22') 仅用于测试。

Right now I use @"(Designation BEGINSWITH '22')" for testing only.

这是我的NSFetchedResultsController:

This is my NSFetchedResultsController:

    - (NSFetchedResultsController *)fetchedResultsController {

    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:@"Product" inManagedObjectContext:importContext];//self.managedObjectContext
    [fetchRequest setEntity:entity];

    // Set the batch size to a suitable number.
    [fetchRequest setFetchBatchSize:20];

    // Edit the sort key as appropriate.
    NSSortDescriptor *sortDescriptor = [[NSSortDescriptor alloc] initWithKey:@"Designation" ascending:NO];
    NSArray *sortDescriptors = [[NSArray alloc] initWithObjects:sortDescriptor, nil];

    [fetchRequest setSortDescriptors:sortDescriptors];

    // Edit the section name key path and cache name if appropriate.
    // nil for section name key path means "no sections".
    NSFetchedResultsController *aFetchedResultsController = [[NSFetchedResultsController alloc] initWithFetchRequest:fetchRequest managedObjectContext:importContext sectionNameKeyPath:nil cacheName:@"Root"]; //self.managedObjectContext

    aFetchedResultsController.delegate = self;
    self.fetchedResultsController = aFetchedResultsController;

    [aFetchedResultsController release];
    [fetchRequest release];
    [sortDescriptor release];
    [sortDescriptors release];

    return fetchedResultsController_;
}    

问题是无论如何或是否获取的数据都保持不变我设定一个谓词。如果我要在 viewDidLoad 中设置谓词,它将起作用,但是如果再次尝试,则无法获得新结果。我使用 importContext 批量导入我的CoreData。

The problem is that the fetched data stays the same, no matter how or if I set a predicate. If I would set a predicate in the viewDidLoad it would work, but then I wouldn't be able to get new results if I'd tried that again. I use an "importContext" for a batch-import of my CoreData.

有什么想法吗?

谢谢!

更新:
好​​的,这是我发现的。我有一个 importContext 在其中进行批量导入。对于 fetchController ,我使用 self.managedObjectContext 。这就是为什么它不起作用的原因,所以我必须使 self.managedObjectContext 与我的 importContext 具有相同的内容...

UPDATE: Ok, here is what I found out. I have an importContext where I make a batch-import. And for the fetchController I use self.managedObjectContext. That's why it doesn't work, so I have to make self.managedObjectContext have the same stuff as my importContext somehow...

推荐答案

您正在使用缓存(@ Root)…尝试将其设置为nil,这可能会阻止崩溃。您不应该缓存旨在作为谓词的FetchedResultsControllers。

You're using a cache (@"Root")… Try setting it to nil, this could prevent crashes. You should not cache FetchedResultsControllers meant to be Predicated.

这篇关于NSFetchedResultsController与NSPredicate不更新的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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