Core data 检索关系coredata中的值

Core data 检索关系coredata中的值,core-data,ios5,xcode4.3,Core Data,Ios5,Xcode4.3,我一直在制作一个使用核心数据的程序。它有两个实体,医学和日志 药物有一套日志 我让它在第一次添加药物时添加日志。此日志包含日期。 我想显示药物的最新日期可以有多个日志,但不知道如何检索相应的日志。我找到的教程只显示了如何添加,而没有显示如何检索 这是我目前所拥有的,它显示了药物名称。我想更改detailtextlabel以显示最新日志条目的日期 - (void)configureCell:(UITableViewCell *)cell atIndexPath:(NSIndexPath *)ind

我一直在制作一个使用核心数据的程序。它有两个实体,医学和日志 药物有一套日志

我让它在第一次添加药物时添加日志。此日志包含日期。 我想显示药物的最新日期可以有多个日志,但不知道如何检索相应的日志。我找到的教程只显示了如何添加,而没有显示如何检索

这是我目前所拥有的,它显示了药物名称。我想更改detailtextlabel以显示最新日志条目的日期

- (void)configureCell:(UITableViewCell *)cell atIndexPath:(NSIndexPath *)indexPath
{
    NSManagedObject *object = [self.fetchedResultsController objectAtIndexPath:indexPath];
    NSLog(@"%@",object);
    cell.textLabel.text = [object valueForKey:@"name"];
    cell.detailTextLabel.text = [[object valueForKey:@"active"] stringValue];
}

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

    NSFetchRequest *fetchRequest = [[NSFetchRequest alloc] init];
    // Edit the entity name as appropriate.
    NSEntityDescription *medicines = [NSEntityDescription entityForName:@"Medicines" inManagedObjectContext:self.managedObjectContext];
    [fetchRequest setEntity:medicines];

    // Set the batch size to a suitable number.
    [fetchRequest setFetchBatchSize:20];

    // Edit the sort key as appropriate.
    NSSortDescriptor *sortDescriptor1 = [[NSSortDescriptor alloc] initWithKey:@"active" ascending:NO];
    NSSortDescriptor *sortDescriptor2 = [[NSSortDescriptor alloc] initWithKey:@"name" ascending:NO];
    NSArray *sortDescriptors = [NSArray arrayWithObjects:sortDescriptor1, sortDescriptor2, nil];

    [fetchRequest setSortDescriptors:sortDescriptors];

    // Edit the section name key path and cache name if appropriate.
    // nil for section name key path means "no sections".
    NSFetchedResultsController *aFetchedResultsController = [[NSFetchedResultsController alloc] initWithFetchRequest:fetchRequest managedObjectContext:self.managedObjectContext sectionNameKeyPath:nil cacheName:@"Master"];
    aFetchedResultsController.delegate = self;
    self.fetchedResultsController = aFetchedResultsController;

    NSError *error = nil;
    if (![self.fetchedResultsController performFetch:&error]) {
         // Replace this implementation with code to handle the error appropriately.
         // abort() causes the application to generate a crash log and terminate. You should not use this function in a shipping application, although it may be useful during development. 
        NSLog(@"Unresolved error %@, %@", error, [error userInfo]);
        abort();
    }

    return __fetchedResultsController;
} 

您提供的设置细节很少,因此很难给出详细的答案

让我们告诉你,你有这样的东西:

Medicine:
- name: string
- logs: ->> Log

Log:
- medicine: -> Medicine
- date: NSDate
在某个时刻,您有一个NSFetch请求。应该是这样的:

NSFetchRequest* fetchRequest = [NSFetchRequest fetchRequestWithEntityName: @"Log"];
fetchRequest.predicate = [NSPredicate predicateWithFormat: ...
现在魔术开始了

fetchRequest.sortDescriptors = @[
    [NSSortDescriptor sortDescriptorWithKey: @"date" ascending: NO]
];
结果集中的第一个条目将是最新的。”fetchedLogs'.firstObject.date将返回最近的日志输入日期

正如我所说的,我不能给你一个更精确的答案,因为你的问题不是很详细

编辑:哦,你在我写这篇文章的时候添加了一些细节。好吧,我不知道药物和日志条目之间的关系如何。可能和我上面的建议很接近。因此,您所需要的是:

[medicine.logs valueForKeyPath: @"@max.date"];
这将返回日志集中的最高日期


编辑:哎呀,我的坏消息,valueForKeyPath,而不仅仅是valueForKey。

你能提供一些关于你的fetchedResultsController的详细信息吗?目前它只提供药品。我读过一些页面,上面说我也需要以同样的方式获取第二个表,还有一些页面说我可以使用nsset日志获取第二个表,这与我的设置非常相似。目前唯一重要的字段是:Medicine:name-stringlogs-nssetI应该添加@max.date,这是获取所需内容的最简单方法,但肯定不是最快的。如果Log.date存在索引,则定制的NSFetchRequest速度会快得多;日志具有药物属性非原子,保留药物*药物;好吧,我想我得到这个是因为我没有检索到任何日志,或者至少这是互联网搜索告诉我的,当我试图打印出对象2012-06-11 17:29:12.379 Medicine Tracker[10766:fb03]托管对象0x6d33990实体:Medicines上的关系“日志”错误时得到的;id:0x6d32380;数据:{active=1;amountToTake=1;hoursBetween=1;log=;name=Med;}