重新排序 UITableView 时,NSFetchedResultsController 没有最新状态
我有一个由 NSFetchedResultsController
提供支持的 UITableView
,并且正在尝试正确支持重新排序。
在 moveRowAtIndexPath:(NSIndexPath *)fromIndexPath toIndexPath:(NSIndexPath *)toIndexPath
内部,我通过更新 NSManagedObject
上的 order
值来响应重新排序并像这样保存上下文:
[self.fetchedResultsController.managedObjectContext save:nil];
现在所有对象都有正确的 order
值,并且结果控制器的 NSFetchRequest
按该值正确排序。
这似乎确实有效,但是当我在表视图中上下滚动时,会显示旧值。所以看起来当我在 NSFetchedResultsController
上使用 objectAtIndexPath
时,它不会返回最新的对象。
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
// ... cell generation code ...
NSManagedObject *obj = [self.fetchedResultsController objectAtIndexPath:indexPath];
[cell.textLabel setText:sideEffect.name];
}
如果我离开然后返回,单元格的顺序是正确的。
调用 save
后,我是否需要:
- 刷新
NSFetchedResultsController
- 使缓存无效
- 刷新/重置
UITableView
上的某些内容?
I have a UITableView
powered by an NSFetchedResultsController
and am trying to properly support reordering.
Inside moveRowAtIndexPath:(NSIndexPath *)fromIndexPath toIndexPath:(NSIndexPath *)toIndexPath
I respond to the reordering by updating an order
value on the NSManagedObject
s and save the context like this:
[self.fetchedResultsController.managedObjectContext save:nil];
So now all objects have the correct order
value and the results controller's NSFetchRequest
sorts by that value correctly.
This does appear to work, however when I scroll up and down in the table view, the old values are being displayed. So it looks like when I use objectAtIndexPath
on the NSFetchedResultsController
, it doesn't return the latest object.
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
// ... cell generation code ...
NSManagedObject *obj = [self.fetchedResultsController objectAtIndexPath:indexPath];
[cell.textLabel setText:sideEffect.name];
}
If I navigate away and then return, the cells are in the correct order.
After my call to save
, do I need to:
- Refresh the
NSFetchedResultsController
- Invalidate a cache
- Refresh/reset something on the
UITableView
?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
解决方案是在移动修改发生后重新初始化获取控制器。由于我有一个特殊的 getter 方法来生成控制器(并执行第一次获取),因此我只需将实例变量设置为 nil 即可使其在下次需要时重新生成。
The solution was to reinitialise the fetch controller after the move modifications had taken place. Since I had a special getter method generating the controller (and performing the first fetch), I could just set the instance variable to
nil
to cause it to be regenerated next time it was needed.