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 
相关问题