2012-06-11 30 views
0

我一直在製作一個使用核心數據的程序。它有兩個實體,醫藥和日誌 藥品有一套日誌檢索關係coredata中的值

我有它添加一個日誌藥物時,它的第一次加入。這個日誌包含一個日期。 我想顯示藥物的最新日期(可以有多個日誌),但不知道如何檢索相應的日誌。我發現的教程只顯示如何添加但不能檢索。

這是我到目前爲止,它顯示藥物名稱。我想改變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; 
} 
+0

你能否提供一些關於你的* fetchedResultsController *的細節? –

+0

目前它只能獲得藥品。我讀過一些頁面,說我需要以同樣的方式獲得第二張表格,其他人只是說我可以使用nsset日誌 –

回答

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。

+0

這幾乎是我的設置。目前唯一重要的字段是: Medicine: 名稱 - 字符串 日誌 - nsset –

+0

我應該添加@ max.date是獲取所需內容的最簡單方法,但肯定不是最快的。如果Log.date存在索引,則定製的NSFetchRequest將更快。 –

+0

這種藥有日誌 的NSSet中 「屬性(非原子,保留)的NSSet *登陸;」 登錄有醫學 「屬性(非原子,保留)藥品*藥;」 –