0

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

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

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

#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 正在做他的工作。我错过了什么?期待中的感谢。

4

2 回答 2

0

这似乎现在起作用了!我不确定发生了什么变化!– 穆斯塔法 2010 年 9 月 6 日 4:03

于 2011-11-24T09:57:43.617 回答
0

不确定我是否了解您的设置,但...

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

于 2010-08-11T19:22:02.617 回答