检索关系coredata中的值

时间:2012-06-11 14:23:20

标签: core-data ios5 xcode4.3

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

我在第一次添加药物时添加了日志。此日志包含日期。 我想显示药物的最新日期(可能有多个日志),但不知道如何检索相应的日志。我发现的教程只显示如何添加但不能检索。

这是我到目前为止,它显示药物名称。我想更改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;
} 

1 个答案:

答案 0 :(得分:0)

你提供的设置细节很少,所以很难给你一个详细的答案。

让我们告诉你有类似的东西:

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

Log:
- medicine: -> Medicine
- date: NSDate

在某些时候,你有一个NSFetchRequest。它应该是这样的:

NSFetchRequest* fetchRequest = [NSFetchRequest fetchRequestWithEntityName: @"Log"];
fetchRequest.predicate = [NSPredicate predicateWithFormat: ...

现在魔术开始了。

fetchRequest.sortDescriptors = @[
    [NSSortDescriptor sortDescriptorWithKey: @"date" ascending: NO]
];

结果集中的第一个条目将是最新的。 'fetchedLogs'.firstObject.date将返回最近的日志输入日期。

正如我所说,由于你的问题不是很详细,我不能给你一个更有针对性的答案。

编辑:哦,你在写这篇文章时添加了一些细节。好吧,我不知道药品和日志条目之间的关系是怎样的。可能与我上面提到的有些接近。所以你需要的只是:

[medicine.logs valueForKeyPath: @"@max.date"];

这将返回日志集中的最高日期。

编辑:oops,我的错,valueForKeyPath,而不仅仅是valueForKey。

相关问题