简体   繁体   English

将NSManagedObject更新为核心数据

[英]Update NSManagedObject into Core Data

So I have a tabbed application. 所以我有一个选项卡式的应用程序。 The first tab allows a user to enter information in ~20 fields that describe a NSManagedObject. 第一个选项卡允许用户在约20个描述NSManagedObject的字段中输入信息。 They are then able to save this into core data, and that works just fine. 然后,他们可以将其保存到核心数据中,并且效果很好。

The second tab is a TableView of all of the existing submissions. 第二个选项卡是所有现有提交的TableView。 Now when a user clicks on a cell in the TableView, it will open up the first tab and repopulate all of the fields that were originally saved into core data. 现在,当用户单击TableView中的单元格时,它将打开第一个选项卡,并重新填充最初保存到核心数据中的所有字段。 When the user clicks save again, I want the existing submission in core data to be updated, instead of a new insertion into core data. 当用户再次单击“保存”时,我希望更新核心数据中的现有提交,而不是新插入核心数据中。

I have found a lot of information saying that I should make a fetch request and then update it like that. 我发现很多信息说我应该发出获取请求,然后像这样更新它。 But that seems redundant to me because I already have the object that was saved passed to the first tab/ViewController. 但这对我来说似乎是多余的,因为我已经将保存的对象传递到了第一个选项卡/ ViewController。

If you could point me to some code that would help my situation or describe a way you might accomplish this scenario, I would greatly appreciate it! 如果您可以向我指出一些可以帮助解决我的情况的代码,或者描述可以完成此情况的方法,我将不胜感激!

Since you have a reference to the NSManagedObject in the first tab, you can update its properties to the new values when the user saves. 由于在第一个选项卡中具有对NSManagedObject的引用,因此可以在用户保存时将其属性更新为新值。 You can then save the changes to your NSManagedObject (let's call it myObject for simplicity) by calling [[myObject managedObjectContext] save:&error] where error is an NSError * . 然后,您可以通过调用[[myObject managedObjectContext] save:&error]其中errorNSError * )将更改保存到NSManagedObject (为简单起见,将其称为myObject )。

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

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