簡體   English   中英

核心數據 - 具有一對多關系的sectionNameKeyPath

[英]Core Data - sectionNameKeyPath with a One to Many Relationship

我在使用關系創建tableView部分時遇到困難。

我有兩個實體,其關系列表為 << ----- >> Item 在此輸入圖像描述

我希望List為Section, Item為行。 我設置了sectionNameKeyPath用鑰匙路徑@"itemList" 這就是我的fetchedResultsController的其余內容

- (NSFetchedResultsController *)fetchedResultsController {

    if (_fetchedResultsController != nil) {
        return _fetchedResultsController;
    }

    // Fetch Request
    NSFetchRequest *fetchRequest = [[NSFetchRequest alloc] initWithEntityName:@"Item"];
    [fetchRequest setFetchBatchSize:20];

    // Sort Descriptors
    NSSortDescriptor *itemSort = [[NSSortDescriptor alloc] initWithKey:@"displayOrderItem" ascending:YES];
    NSSortDescriptor *sectionSort = [[NSSortDescriptor alloc] initWithKey:@"displayOrderList" ascending:YES];
    NSArray *sortDescriptors = @[sectionSort, itemSort];
    [fetchRequest setSortDescriptors:sortDescriptors];

    // Fetched Results Controller
    NSFetchedResultsController *aFetchedResultsController = [[NSFetchedResultsController alloc] initWithFetchRequest:fetchRequest managedObjectContext:self.managedObjectContext sectionNameKeyPath:@"itemList" cacheName:nil];
    aFetchedResultsController.delegate = self;
    self.fetchedResultsController = aFetchedResultsController;

    NSError *error;
    if (![self.fetchedResultsController performFetch:&error]) {
        NSLog(@"Unresolved error %@, %@", error, [error userInfo]);
    }

    return _fetchedResultsController;
}

結果是fetchedResultsController根本沒有填充tableView。 當我嘗試沒有部分時,使用sectionNameKeyPath:nilsetSortDescriptor:itemSort ,它會填充tableView。 此外, numberOfSectionsInTableViewcontroller didChangeSection已正確設置。

我不確定我做錯了什么。 任何人都可以幫我嗎?

謝謝

將段名稱鍵路徑更改為itemList.listName因為FRC期望該段的字符串名稱,而不是“表示”該段的托管對象。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM