【问题标题】:NSFetchedResultsController with NSPredicate not updating带有 NSPredicate 的 NSFetchedResultsController 未更新
【发布时间】:2010-12-21 20:30:53
【问题描述】:

我从核心数据库中获取数据并将它们呈现在UITableView 中。我使用NSFetchedResultController 来获取数据。我希望用户能够在数据库中搜索,为此我使用NSPredicate

数据显示在UITableView 中,一切正常,直到我将NSPredicate 设置为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

这是在搜索开始时完成的:

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];

现在我只使用@"(Designation BEGINSWITH '22')" 进行测试。

这是我的 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。

有什么想法吗?

提前致谢!

更新: 好的,这就是我发现的。我有一个importContext,我在其中进行批量导入。对于fetchController,我使用self.managedObjectContext。这就是它不起作用的原因,所以我必须让self.managedObjectContext 以某种方式与我的importContext 具有相同的东西......

【问题讨论】:

    标签: core-data ios nsfetchedresultscontroller nspredicate


    【解决方案1】:

    您正在使用缓存 (@"Root")...尝试将其设置为 nil,这样可以防止崩溃。你不应该缓存 FetchedResultsControllers 意味着是 Predicated。

    【讨论】:

    • 谢谢!我假期回来了,我终于开始工作了,谢谢你!另外,对于那些有类似问题的人,我可以说我在不同的线程中将内容导入到我的核心数据并使用 NSNotification。像魅力一样工作。
    【解决方案2】:

    比将缓存设置为nil 更好的是给它一个唯一的名称。发布此问题后 iPhone - Cache name for NSFetchedResultsController 我添加了一个函数来生成 UUID 作为我的缓存名称。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-11-23
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多