0

我正在使用NSFetchedResultsController從Core Data填充UITableView。這些數據來自Mogenerator生成的名爲MenuItem的NSManagedObject子類。 MenuItem實體的SectionID參數是NSNumber,這用於確定該項目應在表格視圖內的哪個部分。Mogenerator with NSFetchedResultsController - 0部分

我對數據執行了測試提取並確認核心數據已正確填充。一切都很好。

NSFetchedResultsController按如下方式創建和sectionKeyNamePath設置爲@ 「sectionId」:

NSFetchRequest *fetchRequest = [[NSFetchRequest alloc] init]; 
NSEntityDescription *entity = [NSEntityDescription entityForName:@"MenuItem" inManagedObjectContext:self.persistenceController.moc]; 
[fetchRequest setEntity:entity]; 
fetchRequest.sortDescriptors = @[[NSSortDescriptor sortDescriptorWithKey:@"sectionId" ascending:YES], [NSSortDescriptor sortDescriptorWithKey:@"rowId" ascending:YES]];  
NSFetchedResultsController *frc = nil; 
frc = [[NSFetchedResultsController alloc] initWithFetchRequest:fetchRequest 
              managedObjectContext:self.persistenceController.moc 
              sectionNameKeyPath:@"sectionId" 
                cacheName:nil]; 

的問題(和溶液):

利用該代碼,這些部分沒有被識別。 NSFetchedResultsController總是返回0.這在以前我手動創建NSMO子類時工作過,所以我認爲它與Mogenerator有關。

如果我將sectionNameKeyPath更改爲@「primitiveSectionId」,那麼它就起作用。

希望這將有助於未來的某個人,但我不明白這裏發生了什麼。請有人可以解釋爲什麼這解決了這個問題?

謝謝

回答

0

你爲什麼設置FRC爲零?試試這個(它適用於我使用NSNumber屬性):

使用mogenerator,您可以使用「[MenuItem entityName]」獲取實體,並使用MenuItemAttributes.sectionId訪問屬性。

NSFetchRequest *fetchRequest = [NSFetchRequest fetchRequestWithEntityName:[MenuItem entityName]]; 
fetchRequest.sortDescriptors = @[[NSSortDescriptor sortDescriptorWithKey:MenuItemAttributes.sectionId ascending:YES], [NSSortDescriptor sortDescriptorWithKey:MenuItemAttributes.rowID ascending:YES]]; 
NSFetchedResultsController *frc = [[NSFetchedResultsController alloc] initWithFetchRequest:fetchRequest 
                     managedObjectContext:self.persistenceController.moc 
                     sectionNameKeyPath:MenuItemAttributes.sectionId 
                       cacheName:nil]; 

// Make sure you set your frc as the delegate 
frc.delegate = self; 

NSError *error = nil; 
if (![fetcher performFetch:&error]) { 
    /* 
    * Did you execute the request? 
    */ 
    NSLog(@"Unresolved error %@, %@", error, [error userInfo]); 
    abort(); 
} 
NSLog(@"%lu", [frc.sections count]); 
相關問題