简体   繁体   English

核心数据NSManagedObject更改

[英]Core Data NSManagedObject Changes

I am using a simple Master Detail scenario where the master uses a fetched results controller to populate its tableview. 我正在使用一个简单的Master Detail场景,其中master使用获取的结果控制器来填充其tableview。 When a row is selected, the respective NSManagedObject is passed to the detail view controller where it is used to populate a few UILabels. 选择行时,相应的NSManagedObject将传递到详细信息视图控制器,在那里它用于填充一些UILabel。 I added an EditorViewController to allow some of the fields to be updated by the user. 我添加了一个EditorViewController,允许用户更新一些字段。 On save, I dismiss the EditorViewController and go back to the Detail view controller. 在保存时,我关闭EditorViewController并返回Detail视图控制器。 The save uses the context assigned to the NSManagedObject. 保存使用分配给NSManagedObject的上下文。

What is the best way to know that the object in question changed so I can update the UI in the Detail VC? 知道有问题的对象发生了变化的最佳方法是什么,以便我可以更新Detail VC中的UI? Currently, if I try to update the UILabels in the Detail VC, I don't see any of the new values for the object's properties. 目前,如果我尝试更新Detail VC中的UILabel,我没有看到对象属性的任何新值。 I know a delegate/protocol scenario would likely work but I am curious if there something already in place that I can use – a notification I can look for to update the object or if I should have a fetched results controller on the details screen? 我知道委托/协议方案可能会有效,但我很好奇是否有我可以使用的东西 - 我可以寻找更新对象的通知,或者我是否应该在详细信息屏幕上有一个获取结果控制器? Not sure the proper path to take here. 不确定在这里采取正确的道路。

NSFetchedResultsController is a most common solution to this problem. NSFetchedResultsController是此问题的最常见解决方案。 It listens for changes in the context and updates the table view's content accordingly. 它会侦听上下文中的更改并相应地更新表视图的内容。 Your question is rather general, so I'll leave a link to a very good article showing how to use it properly . 你的问题相当笼统,所以我会留下一篇很好的文章链接,展示如何正确使用它

You can use NSFetchedResultsController and register fetch request for that object (I think it is good solution). 您可以使用NSFetchedResultsController并为该对象注册获取请求(我认为这是一个很好的解决方案)。 Alternatively you can listen for notifications like NSManagedObjectContextDidSaveNotification , but I would not recommend it. 或者,您可以侦听NSManagedObjectContextDidSaveNotification通知,但我不推荐它。 Or you can pass an object to your EditorViewController and update properties there directly. 或者您可以将对象传递给EditorViewController并直接更新属性。

It's hard to say without seeing any code what you are going for, but the other common pattern for working with CoreData makes use of the Model-View-Controller(MVC) paradigm to stage info from your data store in a class variable, then your interface consumes from that class variable. 很难说没有看到任何代码你想要什么,但使用CoreData的另一个常见模式是使用模型 - 视图 - 控制器(MVC)范例来在数据存储中将信息转换为类变量,然后是接口使用该类变量。 You worry about saving, updating and refreshing the data between CoreData and your class variable separately from your interface consuming that data. 您担心在CoreData和类变量之间保存,更新和刷新数据与使用该数据的接口分开。

A typical workflow would then be: 那么典型的工作流程是:

//User does something that creates new info
//Store that info permanently in CoreData
//Refresh the class Variable by loading from CoreData (you can also skip this by updating the class variable simultaneously while saving to CoreData, but this is really sloppy and problematic. Better to save then re-fetch)
//Update your interface from the class Variable

Alternately, you could try Key Value Observing? 或者,您可以尝试键值观察? but I'm not sure it's right for your situation. 但我不确定它适合你的情况。

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

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