2013-03-09 167 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