简体   繁体   English

核心数据,托管对象和多个表视图控制器

[英]Core data, managed objects and multiple table view controllers

I have a UITabBarController with two tabs. 我有一个带有两个选项卡的UITabBarController Each tab has a UINavigationController and each UINavigationController has a table view. 每个选项卡都有一个UINavigationController ,每个UINavigationController有一个表格视图。 The first tab lets you see and edit a list of all employees. 第一个选项卡使您可以查看和编辑所有员工的列表。 The second tab lets you see a subset of the employees. 第二个选项卡使您可以看到一部分员工。

My problem is that in the first tab you can delete an employee from core data. 我的问题是,在第一个标签中,您可以从核心数据中删除员工。 However, when you go back to the second tab it crashes because it still has a pointer to the deleted employee. 但是,当您返回第二个选项卡时,它会崩溃,因为它仍然具有指向已删除员工的指针。

Is there an easy way to check if an nsmanaged object has been deleted, or is there a way to notify or listen for deletions? 有没有一种简单的方法来检查nsmanaged对象是否已删除,或者有一种方法来通知或侦听删除?

Register for NSManagedObjectContext NSManagedObjectContextDidSaveNotification Call NSFetchedResultsController mergeChangesFromContextDidSaveNotification: 注册NSManagedObjectContext NSManagedObjectContextDidSaveNotification调用NSFetchedResultsController mergeChangesFromContextDidSaveNotification:

Take a look at the Core Data Books sample code. 看看Core Data Books示例代码。

If you are using NSFetchedResultsController instances on each table view then you just need to perform a save in the first tab and respond to the delegate methods on the second. 如果在每个表视图上使用NSFetchedResultsController实例, NSFetchedResultsController需要在第一个选项卡中执行保存操作,然后在第二个选项卡上响应委托方法。

If you are not using a NSFetchedResultsController you should be if you are display a table of data. 如果您不使用NSFetchedResultsController ,则应显示数据表。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM