我有一个应用程序,它有一个包含客户信息的 TableView。
如果点击一个单元格,我会加载该客户的详细信息。
我正在使用 Magical Record 来管理我的所有实体。
我在做什么:
1) Load all the CUSTOMER entity on the viewDidLoad of my TableView.
2) User click to see the detail of the customer
3) Pass to the ViewController of the detail the object that represent the entity CUSTOMER (only one).
一切正常。
问题是,如果用户选择重新加载 TableView,我会这样做:
1) Go to the API to get all the customers
2) [Customer truncateAll]
3) Create all the entities back on CoreData
5) [[NSManagedObjectContext defaultContext] saveToPersistentStoreAndWait]
6) Reload tableview
但是,如果用户在此过程中点击查看客户的详细信息,下面的 ViewController 将暂时正确显示客户信息。然后 [Customer truncateAll] 发生的所有信息都死掉了。
如何防止这种情况发生?
非常感谢!
最佳答案
您可以使用 NSFetchedResultController
来监听您的 Customer
实体更改。当您的 API 调用完成并且您在应用程序的其他地方创建了适当的实体时,您将在 Controller 中收到有关这些更改的通知,然后您可以重新加载数据。如果您不熟悉这个概念,请查看 here .
关于ios - 魔法记录 - 丢失我的实体信息,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23116261/