如何在实体(核心数据)中生成默认记录?
我只是在核心数据中执行第一步,并且想更改我的一段代码,该代码使用一个简单的 plist 来存储程序的一些首选项数据。我想改用核心数据。只是出于教育原因。
所以基本上我创建了一个名为“setup”的实体,其中包含一堆属性,如“语言”、“调试信息”、“服务器 IP”等。实体中应该只有一个数据记录,因为保留多个设置是没有意义的。
然后,我通过按住 alt 键将实体拖动到 GUI 窗口上,在界面生成器中创建了 GUI。我选择了“单项视图”。我可以运行代码,但有一个问题:
我无法更改控件的内容。我认为这只是因为实体中没有记录。所以我的问题是,如何创建“默认”条目?我不想使用“主/详细视图”,因为我只需要一条数据记录,该数据记录应该自动生成,以防尚未创建存储数据文件。
I'm just doing my first steps in Core Data and would like to change a piece of my code which uses a simple plist to store some preference data of a program. I would like to use Core Data instead. Just for educational reason.
So basically I created an entity named "setup" with a bunch of attributes like "language", "debug info", "server ip" and so on. There should be only one data record in the entity as there is no sense to keep multiple setups.
I then created the GUI in the interface builder by alt-dragging the entity over the GUI window. I choosed "single item view". I can run the code but there is a problem:
I'm not able to change the content of the contols. I assume it's simply because there is no record in the entity. So my question is, how to create a "default" entry? I don't want to use the "Master/Detail view" as I simply need just one data record which should be generated automatically in case the storedata-file isn't created yet.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
到目前为止我解决了这个问题。我将以下几行添加到方法“- (NSManagedObjectContext *) ManagedObjectContext”中。它在第一次调用该方法时在实体中创建默认记录。属性的值在数据模型中设置。
我不确定这是否是正确的处理方法,尤其是我怀疑这是正确的地方。我缺少在设置核心数据后调用的方法。非常欢迎任何建议!
I solved the problem so far. I added the following lines to the method "- (NSManagedObjectContext *) managedObjectContext". It creates a default record in the entity on the first call of the method. The values of the attributes are set up in the data model.
I'm not sure if it's the correct way to handle it, especially I doubt that's the right place. I'm missing the method which is called after the Core Data has been set up. Any suggestions are highly welcome!