ios - 核心数据更新记录

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/11279843/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-30 19:04:01  来源:igfitidea点击:

ios - Core Data updating records

objective-cioscore-data

提问by jwknz

I am currently looking through documentation and the web, but I am not seeing what I am looking for and I would just like to confirm if what I am looking for exists:-)

我目前正在浏览文档和网络,但我没有看到我要找的东西,我只想确认我要找的东西是否存在:-)

As I understand it - this line of code allows me to add a record to the SQLite database which I am showing in my app through Core Data:

据我了解 - 这行代码允许我向 SQLite 数据库添加一条记录,我通过 Core Data 在我的应用程序中显示该记录:

NSManagedObject *newSoftware = [NSEntityDescription insertNewObjectForEntityForName:

Now is there a line similar to that that allows me to update the record I am working on?

现在是否有类似于允许我更新我正在处理的记录的行?

something like this?

像这样的东西?

NSManagedObject *newSoftware = [NSEntityDescription updateCurrentObjectForEntityForName:

Thank you very much for the feedback:-)

非常感谢您的反馈:-)

回答by melsam

To update a record, you simply update the NSManagedObject's properties. If you're using CoreData, you should have NSManagedObjects for each of your "tables". Get a reference to an object after you've queried the database (using NSFetchRequest). Then you can update the record like this: myObject.firstName = "John";When you're ready to update the object in your Sqlite database, just call your NSManagedContext's savemethod. I suggest play with a CoreData sample app (like the default one generated by Xcode) to understand this better.

要更新记录,您只需更新NSManagedObject的属性。如果您使用的是 CoreData,则NSManagedObject每个“表”都应该有s。查询数据库后获取对对象的引用(使用NSFetchRequest)。然后你可以像这样更新记录:myObject.firstName = "John";当你准备好更新你的 Sqlite 数据库中的对象时,只需调用你NSManagedContextsave方法。我建议使用 CoreData 示例应用程序(如 Xcode 生成的默认应用程序)以更好地理解这一点。

回答by Lorenzo B

Here my previous answer on it. It is quite detailed to understand what is going on.

这是我之前的回答。了解正在发生的事情非常详细。

How to update existing object in Core Data?

如何更新 Core Data 中的现有对象?

Based on fumoboy007comment, you have to update your object trough KVC if you have not set up a subclass for your managed object. If you haven't already I suggest to create it. The code looks like more cleaner since you can acess a managed object through properties.

根据fumoboy007评论,如果您尚未为托管对象设置子类,则必须通过 KVC 更新您的对象。如果您还没有,我建议您创建它。代码看起来更简洁,因为您可以通过属性访问托管对象。

Here a link on it:

这里有一个链接:

organising-core-data-for-ios

为 ios 组织核心数据