NSFetchedResultsController + sectionNameKeyPath + section order

时间:2010-08-11 05:17:42

标签: core-data nspredicate nsfetchrequest nssortdescriptor

我在两个实体之间有一对多的实体关系:

EntityP (Parent) <-->> EntityC (Child)

属性和关系:

EntityP.title
EntityP.dateTimeStamp
EntityP.PtoC (relationship)

EntityC.title
EntityC.dateTimeStamp
EntityC.CtoP (relationship) // Can be used to get "one" EntityP record

我使用获取结果控制器来显示结果。这是我的fetch结果控制器的实现:

#pragma mark -
#pragma mark Fetched results controller

- (NSFetchedResultsController *)fetchedResultsController {
     // Set up the fetched results controller if needed
     if (fetchedResultsController != nil) {
          return fetchedResultsController;
     }

     // Create the fetch request for the entity
     NSFetchRequest *fetchRequest = [[NSFetchRequest alloc] init];

     // Set Entity
     NSEntityDescription *entity = [NSEntityDescription entityForName:@"EntityC" inManagedObjectContext:self.managedObjectContext];
     [fetchRequest setEntity:entity];

     // Set Predicate
     // (Ignore, we want to get list of all EntityC records)

     // Set Sort Descriptors (sort on Parent - for section, and then on Child - for rows)
     NSSortDescriptor *sortDescriptorPDate = [[NSSortDescriptor alloc] initWithKey:@"CtoP.dateTimeStamp" ascending:YES];
     NSSortDescriptor *sortDescriptorDate = [[NSSortDescriptor alloc] initWithKey:@"dateTimeStamp" ascending:YES];
     NSArray *sortDescriptors = [[NSArray alloc] initWithObjects:sortDescriptorPDate, sortDescriptorDate, nil];

     [fetchRequest setSortDescriptors:sortDescriptors];
     [fetchRequest setFetchBatchSize:20];

     // Create and initialize the fetch results controller
     NSFetchedResultsController *aFetchedResultsController = [[NSFetchedResultsController alloc] initWithFetchRequest:fetchRequest managedObjectContext:self.managedObjectContext sectionNameKeyPath:@"CtoP.title" cacheName:nil];
     aFetchedResultsController.delegate = self;
     self.fetchedResultsController = aFetchedResultsController;

     // Cleanup memory
     [aFetchedResultsController release];
     [fetchRequest release];
     [sortDescriptorPDate release];
     [sortDescriptorDate release];
     [sortDescriptors release];

     NSError *error = nil;
     if (![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. If it is not possible to recover from the error, display an alert panel that instructs the user to quit the application by pressing the Home button.
           */
          NSLog(@"Unresolved error %@, %@", error, [error userInfo]);
          abort();
     }

     return fetchedResultsController;
}     

现在,例如,如果我在持久性存储中有以下数据:

EntityP.dateTimeStamp  EntityP.title    EntityC.dateTimeStamp   EntityC.title
Today                  B                Today                   d
Yesterday              A                Yesterday               a
Today                  B                Yesterday               c
Yesterday              A                Today                   b

注意:昨天和今天是NSDate格式。

然后我应按以下顺序(确切地)获取部分和行:

A
 a
 b

B
 c
 d

但是,排序不是这样的。我按正确的顺序获取行,但这些部分没有订购!我希望sortDescriptorPDate正在完成他的工作。我错过了什么?感谢期待。

2 个答案:

答案 0 :(得分:0)

不确定我理解您的设置但是......

默认情况下,部分标题是部分名称的大写首字母。如果你想要一个自定义的部分,比如基于日期的部分,你需要继承NSFetchedResultsController,并覆盖各种sectionIndex...方法,以返回正确的部分,部分索引和部分标题。

答案 1 :(得分:0)

这似乎现在正在运作!我不确定是什么改变了! - Mustafa 2010年9月6日4:03