2013-03-09 171 views
0

我有两个实体:如何使用核心数据关系?

Patient 
- firstName 
- lastName 
- scheduledAppointments <---->> Appointment 

Appointment 
- date 
- times 
- scheduledPatient <<----> Patient 

基本上我有一个病人有许多约会。如何在约会实体中设置scheduledPatient?我已经尝试过目前为止:

[self.appointment setScheduledPatient:[self.patientArray objectAtIndex:indexPath.row]]; 

self.appointment.scheduledPatient = [self.patientArray objectAtIndex:indexPath.row]; 

他们在我编辑约会时工作。但是当我添加一个新约会时它会返回一个SIGBRT。

+0

你能分享崩溃日志? – 2013-03-09 10:24:10

回答

0

你的代码似乎是正确的。 因此,我想很可能你没有在.xcdatamodel文件中正确定义反转关系。

据我所知,你有一个一对多的关系。也就是说,一个病人可能有一些预约。因此,约会属于一名患者。为了让这个关系在语义上是正确的,你需要让它知道它们是如何相互关联的。为此,您需要指定关系中每个元素的逆元素。 在下面的图片中,您可以看到一个区域可能具有多个状态,并且状态只属于一个区域。注意连接关系元素的箭头,“多”具有双箭头,“一”具有单箭头。 enter image description here

我相信你很可能忘了在xcdatamodel文件中指定这个。

退房此链接了解更多信息:https://developer.apple.com/library/mac/#documentation/Cocoa/Conceptual/CoreData/Articles/cdRelationships.html

 
Inverse Relationships 
Most relationships are inherently bi-directional. If a Department has a to-many relationship to the Employees that work in a Department, there is an inverse relationship from an Employee to the Department. The major exception is a fetched property, which represents a weak one-way relationship—there is no relationship from the destination to the source (see “Fetched Properties”). 

You should typically model relationships in both directions, and specify the inverse relationships appropriately. Core Data uses this information to ensure the consistency of the object graph if a change is made (see “Manipulating Relationships and Object Graph Integrity”). For a discussion of some of the reasons why you might not want to model a relationship in both directions, and some of the problems that might arise if you don’t, see “Unidirectional Relationships.” 
+0

谢谢!解决了! – msluna 2013-03-09 12:22:42