2012-09-14 80 views
0

我是核心數據的新手,我需要以下問題的幫助:關係錯誤(核心數據)

我有兩個實體:Talk和Spe​​aker。他們有很多關係。

講座 - 目的地:喇叭,逆:談 音箱 - 目的地:談話,反:揚聲器

場「一對多關係」是在兩個實體進行檢查。

我有一個表視圖控制器列出所有可用的會談。這就是爲什麼我做的:

- (NSFetchedResultsController *)fetchedResultsController 
{ 
    if (_fetchedResultsController != nil) { 
     return _fetchedResultsController; 
    } 

    NSFetchRequest *fetchRequest = [[NSFetchRequest alloc] init]; 
    NSEntityDescription *entity = [NSEntityDescription 
            entityForName:@"Talk" inManagedObjectContext:self.managedObjectContext]; 
    [fetchRequest setEntity:entity]; 

    NSSortDescriptor *sort = [[NSSortDescriptor alloc] 
           initWithKey:@"date" ascending:NO]; 
    [fetchRequest setSortDescriptors:[NSArray arrayWithObject:sort]]; 
    [fetchRequest setFetchBatchSize:20]; 

    NSFetchedResultsController *theFetchedResultsController = 
    [[NSFetchedResultsController alloc] initWithFetchRequest:fetchRequest 
             managedObjectContext:self.managedObjectContext sectionNameKeyPath:nil 
                cacheName:@"Root"]; 
    self.fetchedResultsController = theFetchedResultsController; 
    _fetchedResultsController.delegate = self; 

    return _fetchedResultsController; 
} 

當我點擊一個細胞,我想在另一視圖控制器,顯示所選擇的談話只是細節。

這是我在做什麼:

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath 
{ 
    TalksDetailsViewController *talksDetails = [[TalksDetailsViewController alloc] initWithTalksInfo:[_fetchedResultsController objectAtIndexPath:indexPath]]; 
    [self.navigationController pushViewController:talksDetails animated:YES]; 
} 

的問題是:我不能看到TalksDetailsViewController揚聲器。這是發送到TalksDetailsViewController的信息:

Info: <Talk: 0x6e66920> (entity: Talk; id: 0x6e66880 <x-coredata://4063FE84-E6DB-4588-8133-9A55B512D6C8/Talk/p2> ; data: { 
    date = "2012-08-31 03:00:00 +0000"; 
    id = 7; 
    speaker = "<relationship fault: 0x6e994e0 'speaker'>"; 
    "start_time" = "17:14:55"; 
}) 

我試過的東西如下面的代碼,但它並沒有奏效:

for (Speaker *speakerInfo in self.talkInfo.speaker) { 
     NSLog(@"speaker info: %@", speakerInfo); 
} 

它永遠不會進入的。

如果我嘗試登錄self.talkInfo.speaker我得到的東西,如:

Relationship 'speaker' fault on managed object (0x7a76c30) <Talk: 0x7a76c30> (entity: Talk; id: 0x7a76b90 <x-coredata://4063FE84-E6DB-4588-8133-9A55B512D6C8/Talk/p2> ; data: { 
    date = "2012-08-31 03:00:00 +0000"; 
    id = 7; 
    speaker = "<relationship fault: 0x6bc5990 'speaker'>"; 
    "start_time" = "17:14:55"; 
}) 

我看到了一些類似的問題,但未能解決我的問題。

任何提示?

+0

關係錯誤並不意味着錯誤,你有沒有嘗試從揚聲器記錄屬性?看到這個答案:http:// stackoverflow。com/a/8876336/879119 – mkral

+0

試過了,但沒有奏效。根據您指出的答案無法解決我的問題。但是,謝謝。 – hdoria

+0

你能記錄self.talkInfo嗎?所以我可以看到它的結構如何: – mkral

回答

1

首先用SQLite瀏覽器檢查對象(here if you dont have one)。並確保數據存儲在覈心數據中。訪問模擬器文件~/Library/Application\ Support/iPhone\ Simulator/XX/Applications/XXXXXXXXXXX/Documents/XXXX.sqlite。確保ZTALKSZSPEAKER和它的值,這應該對應於Z_PK值在ZSPEAKER。如果它確實關係連接。

你的模型應該是這個樣子:

enter image description here

當你有一個像談話對象,因此:

Talk *talk = [**YourManagedObjectContext** executeFetchRequest:fetchRequest error:&error]; 

你應該能夠抓住它的揚聲器,像這樣:

Speaker *speaker = talk.speaker; 

我知道這與你的有點不同設置,但希望這應該有所幫助。

+0

ZTALKS上沒有ZSPEAKER欄。當我不設置多對多關係時,創建此列。當我設置,列不會被創建。 所有信息來自一個JSON,格式如下: 「日期」: 「2012年11月2日」, 「ID」: 「8」, 「名」: 「Java」 的, 「揚聲器」: [ 「/ API/V1 /揚聲器/ 1 /」, 「/ API/V1 /揚聲器/ 2 /」 ], 我怎樣保存陣列: 如果([值isKindOfClass:[NSArray的類] ]){ NSSet * valueSet = [NSSet setWithArray:value]; [managedObject setValue:valueSet forKey:key]; } – hdoria