更改为NSFetchedResultsController后,UITableView不更新DataSource [英] UITableView not updating DataSource after change to NSFetchedResultsController

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

问题描述

我有 UITableView NSFetchedResultsController 填充。初始提取工作正常。我可以添加,删除,修改等零问题..但我想在表中添加用户定义的排序。我这样做是通过更改 NSFetchedResultsController 来使用不同的 sortDescriptor 集,以及另一个 sectionNameKeyPath 。以下是我更改提取的代码:

I have an UITableView populated by a NSFetchedResultsController. The initial fetch works fine. I can add, remove, modify, etc with zero problems.. But I want to add user-defined sorting to the table. I am doing this by changing the NSFetchedResultsController to use a different sortDescriptor set, and a different sectionNameKeyPath. Here is the code where I change the fetch:

-(void)changeFetchData {
    fetchedResultsController = nil;

    NSFetchRequest *fetchRequest = [[NSFetchRequest alloc] init];
    NSEntityDescription *entity = [NSEntityDescription entityForName:@"Object" inManagedObjectContext:managedObjectContext];
    [fetchRequest setEntity:entity];

    NSString *sortKey = @"sortKey";
    NSString *cacheName = @"myNewCache";
    BOOL ascending = YES;

    NSSortDescriptor *sortDescriptor = [[NSSortDescriptor alloc] initWithKey:sortKey ascending:ascending];
    NSArray *sortDescriptors = [[NSArray alloc] initWithObjects:sortDescriptor, nil];
    [fetchRequest setSortDescriptors:sortDescriptors];

    NSFetchedResultsController *aFetchedResultsController = [[NSFetchedResultsController alloc] initWithFetchRequest:fetchRequest managedObjectContext:managedObjectContext sectionNameKeyPath:sortKey cacheName:nil];
    self.fetchedResultsController = aFetchedResultsController;
    fetchedResultsController.delegate = self;

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

    NSError *error;
    if (![[self fetchedResultsController] performFetch:&error]) {
        // Update to handle the error appropriately.
        NSLog(@"Fetch failed");
        NSLog(@"Unresolved error %@, %@", error, [error userInfo]);
        exit(-1);  // Fail
    }

    [self.tableView reloadData];
}

当我调用此方法时,效果很好。该表立即重新排序以使用新的部分信息和新的排序参数。但是,如果我向数据添加或删除项目,TableView不会更新其信息,从而导致崩溃。我可以NSLog记录fetchedResultsController中对象总数的计数,并看到它增加(和减少)但是如果我NSLog返回值 numberOfRowsInSection 来监视那里的变化,该方法被调用,但值不会改变。得到以下崩溃(添加,但删除一个类似)

When I call this method, it works great. The table immediately re-orders itself to use the new section info, and the new sorting parameters. But if I add or remove items to the data, the TableView doesn't update its info, causing a crash. I can NSLog the count of the total number of objects in the fetchedResultsController, and see it increase (and decrease) but if I NSLog the return values for numberOfRowsInSection to monitor a change there, the method gets called, but the values don't change. The get the following crash (for addition, but the deletion one is similar)

无效更新:第2节中的行数无效。现有的行数更新后的部分(3)必须等于更新前的该部分中包含的行数(3),加上或减去从该部分插入或删除的行数(插入1个,删除0个)。 with userInfo(null)
Invalid update: invalid number of rows in section 2. The number of rows contained in an existing section after the update (3) must be equal to the number of rows contained in that section before the update (3), plus or minus the number of rows inserted or deleted from that section (1 inserted, 0 deleted). with userInfo (null)

如果我重新启动应用程序,我会看到添加的项目,或者看不到已删除的项目,所以我正在修改数据源。

If I restart the app, I see the added item, or do not see the deleted item, so I am modifying the datasource correctly.

任何想法?

推荐答案

老控制器可能还活着。如果是这样,它可能仍然将tableview控制器作为其委托调用,并使用它自己的数据激活表更新。

It's possible that the old controller is still alive. If so, it might still be calling the tableview controller as its delegate and activating the table update using it's own data.

我建议在 numberOfRowsInSection 中记录获取的结果控制器对象,以确认它是否使用新控制器。在分配新控制器之前,应将旧控制器的委托设置为nil。

I would suggest logging the fetched results controller object in numberOfRowsInSection to confirm that it using the new controller. You should set the old controller's delegate to nil before assigning the new one.

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

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