0

我在Core Data中有幾個實體。一個被稱爲EventCluster,用於支撐和構造Event對象。設置具有多個實體的FetchedResultsController

所有我想在我UITableView做的很簡單 - 我想UITableViewSections是相應EventCluster對象和部分的每一行是將由eventID值進行排序的Event對象。

現在我要做的就是這樣的:

- (NSFetchedResultsController *)fetchedResultsController { 

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

    NSFetchRequest *fetchRequest = [[NSFetchRequest alloc] init]; 

    NSEntityDescription *entity = [NSEntityDescription entityForName:@"Event" inManagedObjectContext:_managedObjectContext]; 
    [fetchRequest setEntity:entity]; 

    NSSortDescriptor *sort = [[NSSortDescriptor alloc] initWithKey:@"eventID" ascending:NO]; 
    [fetchRequest setSortDescriptors:[NSArray arrayWithObject:sort]]; 

    [fetchRequest setFetchBatchSize:20]; 

    NSFetchedResultsController *theFetchedResultsController = 
    [[NSFetchedResultsController alloc] initWithFetchRequest:fetchRequest 
             managedObjectContext:_managedObjectContext 
              sectionNameKeyPath:nil 
                cacheName:@"Root"]; 
    self.fetchedResultsController = theFetchedResultsController; 
    _fetchedResultsController.delegate = self; 

    return _fetchedResultsController; 
} 

有沒有在這裏設立了NSFetchedResultsController以包括EventCluster結構的方便的方法?

+0

嘗試將sectionNameKeyPath設置爲@「EventCluster」 – Koen

+0

@Koen,nah,這是行不通的。崩潰:「實體事件不是按照預期的密鑰」EventCluster「符合密鑰值編碼的。 –

+0

他們在你的模型中沒有關係? – Koen

回答

0

您需要查詢的事件,使用sectionNameKeyPath:指向上EventCluster一個自定義的方法,該方法返回一個唯一的NSString每個EventCluster:

parentCluster:@"parentCluster.customMethod" 

,這(我的心臟寫了這個,但你想法)

@implementation EventCluster (CategoryName) 

- (NSString *)customMethod 
{ 
    return self.objectID.URIRepresentation.absoluteString; 
} 

@end 
相關問題