0

コアデータを使ったプログラムを作っています。それには2つのエンティティがあります。MedicineとLogMedicinesには一連のログがあります

最初に追加されたときに、薬にログを追加してもらいます。このログには日付が含まれます。薬の最新の日付(複数のログが存在する可能性があります)を表示したいのですが、適切なログを取得する方法がわかりません。私が見つけたチュートリアルは、追加する方法だけを示しており、取得する方法は示していません。

これは私がこれまでに持っているもので、薬の名前が表示されます。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;
} 
4

1 に答える 1

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"];

これにより、ログセットの最高の日付が返されます。

編集:おっと、私の悪い、valueForKeyPathだけでなくvalueForKeyPath。

于 2012-06-11T15:30:26.513 に答える