CoreData - 一对多建模关系是一对一的 [英] CoreData - one-to-many modeled relationship comes out as one-to-one

查看:113
本文介绍了CoreData - 一对多建模关系是一对一的的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是Core Data建模的新手,我很难理解一对多关系是如何工作的。



我有一个名为 Task 的父实体,它可以有多个实例 / code>实体。我这样建模:注释,与的关系任务调用任务任务实体的目的地。在任务上,一个关系称为注释注释它的目的地,和这两个关系彼此相反。



不定义警告或错误消息的相反结果。虽然建模这种方式工作,我注意到,一旦我为给定的任务创建第二个注释,第一个被替换(一对一的关系)。



告诉核心数据模型这种关系允许在一个任务中有多个注释的正确方法是什么?



此外,由于CoreData似乎单独管理主键,我将如何创建 NSPredicate

解决方案

>首先,您需要在任务实体中设置复数选项,然后在 .xdatamodeled 并选择任务实体的关系属性为 comments ,您应该可以看到此



有一个复数选项,一定要检查出来。如果你使用生成的类,并且如果你使用sqlite存储,你必须删除和重建,你必须重新创建你的 NSManagedObject ,所以它不会抱怨新模式不一样的



要检查您是否有一对多的关系,您的任务实体应该有一个名为comments的属性这是 NSSet 类类型



如果要检索给定任务的所有注释,需要重复该任务的 NSSet(comments)属性。


I am new to Core Data modeling, and I am having a hard time understanding how one-to-many relationships work.

I have a parent entity called Task, which can have several instances of Comment entity. I modeled it like this: on Comments, a relationship to Task called task with the Task entity a destination. On Task, a relationship called comments, with Comment as its destination, and both relationships are each others inverse.

Not defining an inverse results in either warnings or error messages. While modeling this way works, I've noticed that once I create a second comment for a given Task, the first is replaced (one-to-one relationship).

What would be the correct way to tell the Core Data Model that this relationship allows many comments in one Task?

Also, since CoreData seems to manage primary keys on its own, how would I create an NSPredicate to retrieve all comments for a given Task?

Thanks for any suggestions!

解决方案

First of all you need to set the plural option in the Task entity, select it in your .xdatamodeled and select the relationship property of Task entity to comments you should be able to see this

there is a plural option be sure to check that out. You must recreate your NSManagedObject if your using generated classes and also if your using sqlite store you must delete and rebuild so it will not complain about the new schema not being the same with the old one.

To check if you have a one to many relationship your Task entity should have a property called comments which is a class type of NSSet not Comments.

If you want to retrieve all comments for a given task you need to iterate the NSSet(comments) property of that task.

这篇关于CoreData - 一对多建模关系是一对一的的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆