2012-12-13 74 views
1

好吧,我成立了一個反身關係的實體(與一個[SO問題]我剛纔1發佈幫助核心數據和反射關係的實體

我相信我將它設置正確:

enter image description here

我有一個實體GroupMember我創建了一個多到許多關係到自身

我再補充一個實體,創建一個「子」實體並將其設置爲關係:。

GroupMember *groupMember = [NSEntityDescription insertNewObjectForEntityForName:@"GroupMember" inManagedObjectContext:context]; 
groupMember.name = @"Ted"; 

GroupMember *subGroupMember = [NSEntityDescription insertNewObjectForEntityForName:@"GroupMember" inManagedObjectContext:context]; 
subGroupMember.name = @"sub member bill"; 

[groupMember addSub_member_relObject:subGroupMember]; 

然後,我爲任何GroupMember實體提出一個獲取請求,以查看是否設置了關係。我回來的是:

fetch objects: (
    "<GroupMember: 0x83cf280> (entity: GroupMember; id: 0x98686b0 <x-coredata://D9082F84-03D9-408D-B0DA-5C75C1B905AB/GroupMember/p1> ; data: {\n \"member_rel\" =  (\n );\n name = Ted;\n \"sub_member_rel\" =  (\n  \"0x989f390 <x-coredata://D9082F84-03D9-408D-B0DA-5C75C1B905AB/GroupMember/p2>\"\n );\n})", 
    "<GroupMember: 0x83cf4a0> (entity: GroupMember; id: 0x989f390 <x-coredata://D9082F84-03D9-408D-B0DA-5C75C1B905AB/GroupMember/p2> ; data: {\n \"member_rel\" =  (\n  \"0x98686b0 <x-coredata://D9082F84-03D9-408D-B0DA-5C75C1B905AB/GroupMember/p1>\"\n );\n name = \"sub member bill\";\n \"sub_member_rel\" =  (\n );\n})" 
) 
2012-12-13 12:08:45.352 [9539:14003] name: Ted 
2012-12-13 12:08:45.355 [9539:14003] set: Relationship 'sub_member_rel' on managed object (0x83cf280) <GroupMember: 0x83cf280> (entity: GroupMember; id: 0x98686b0 <x-coredata://D9082F84-03D9-408D-B0DA-5C75C1B905AB/GroupMember/p1> ; data: { 
    "member_rel" =  (
    ); 
    name = Ted; 
    "sub_member_rel" =  (
     "0x989f390 <x-coredata://D9082F84-03D9-408D-B0DA-5C75C1B905AB/GroupMember/p2>" 
    ); 
}) with objects {(
    <GroupMember: 0x83cf4a0> (entity: GroupMember; id: 0x989f390 <x-coredata://D9082F84-03D9-408D-B0DA-5C75C1B905AB/GroupMember/p2> ; data: { 
    "member_rel" =  (
     "0x98686b0 <x-coredata://D9082F84-03D9-408D-B0DA-5C75C1B905AB/GroupMember/p1>" 
    ); 
    name = "sub member bill"; 
    "sub_member_rel" =  (
    ); 
}) 
)} 

A)我不太明白如何閱讀這個輸出。什麼是「與對象」部分?與關係的實體是什麼? 「子員賬單」是否正確設定爲子員關係? B)當我試圖找回groupMember.Sub_member_rel時,我得到一個NSSet。我如何迭代這些對象以獲取子成員名稱(以在表格視圖中顯示)?

回答

2

A)打印NSManagedObject或其關係的輸出通常冗長且有點複雜。在你的情況下,首先打印關係所屬的對象。然後是「具有對象」部分,它由該特定關係中的對象組成。

B)在To-Many關係中,關係類型始終爲NSSet。就像使用NSArray您可以通過NSSet這樣的循環:

for (GroupMember *subGroupMember in groupMember.sub_member_rel) { 
    // Do something with managedObject 
} 
+1

如果你需要一個數組作爲表視圖數據源,您通過得到它'的NSArray *子成員= [groupMember.sub_member_rel allObjects]'。 –

+0

哈!我正在設置sub_member_rel到一個NSSet,我不能通過這個。好的,這表明我的邏輯存在一個缺陷,我看到「子成員賬單」與子成員賬單的子成員賬單。/boggle感謝您的幫助! – Padin215