我是核心数据的新手,需要帮助解决以下问题:
我有两个实体:Talk 和 Speaker。他们有一对多的关系。
谈话-目的地:演讲者,反向:谈话演讲者-目的地:谈话,反向:演讲者
在两个实体上都选中了“多对多关系”字段。
我有一个表格视图控制器来列出所有可用的谈话。这就是我的做法:
- (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);
}
它永远不会进入 for。
如果我尝试记录 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";
})
我看到了一些类似的问题,但无法解决我的问题。
有小费吗?