复制(复制)核心数据记录和相关记录
我正在编写一个应用程序,允许用户创建“问卷”,然后向其中添加问题。我正在使用核心数据来存储信息。我创建了一个调查问卷实体,并与问题实体具有“一对多”关系。我的问题是,如果想要允许用户复制(复制)整个调查问卷、问题和所有内容,最好的方法是什么?
所以要明确:
我有:
问卷调查记录 --->问题 1 记录 - 问题 2 记录 - 问题 3 记录
我希望用户能够复制此记录并最终得到以下结果:
问卷记录 --->问题1记录-问题2记录-问题3记录
问卷(复印件)记录--->问题1记录 - 问题2记录 - 问题3记录
I am writing an app that allows the user to create a "questionnaire", then add questions to it. I am using core data to store the info. I have created a questionnaire entity and have a "one to many" relationship with question entities. My question is, if want to allow the user to duplicate (copy) an entire questionnaire, questions and all what's the best way to do that?
So to be clear:
I have:
Questionnaire record ---> Question 1 record - Question 2 record - Question 3 record
and I want the user to be able to duplicate this and end up with this:
Questionnaire record ---> Question 1 record - Question 2 record - Question 3 record
Questionnaire (copy) record ---> Question 1 record - Question 2 record - Question 3 record
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您说您想复制整个调查问卷,所以假设您想要的实际上是这样的:
调查问卷 --->问题1-问题2-问题3
调查问卷(复印件)--->问题1(副本) - 问题2(副本) - 问题3(副本)
那么你需要的是深副本。假设您在数据模型上正确设置了关系的所有权规则,您可以基于此 NSManagedObject 类别编写一些内容。
You said you want to duplicate an entire questionnaire, so assuming what you want is actually something like this:
Questionnaire ---> Question 1 - Question 2 - Question 3
Questionnaire (copy) ---> Question 1 (copy) - Question 2 (copy) - Question 3 (copy)
Then what you need is a deep copy. You can write something based on this NSManagedObject category, assuming you set your relationship's ownership rules properly on your data model.
您只需插入一个新的托管对象,然后将其所有值设置为与原始对象相同的值。如果您为托管对象使用专门的子类,您甚至可以编写一个简单的辅助方法(示例假设托管对象名为 Questionnaire,相应更改):
You just insert a new managed object and then set all its values to the same thing as the original object. If you're using a specialized subclass for your managed object, you could even write a simple helper method (example assumes managed object is named Questionnaire, change accordingly):