Objective c 在NSFetchedResultsController中使用sectionNameKeyPath时的顺序

Objective c 在NSFetchedResultsController中使用sectionNameKeyPath时的顺序,objective-c,uitableview,core-data,nspredicate,nsfetchedresultscontroller,Objective C,Uitableview,Core Data,Nspredicate,Nsfetchedresultscontroller,在核心数据中,我有一对多关系-每个布局有许多行: 我试图通过分组到布局来显示表视图中的数据: Layout Nr 2342 (with orderPosition 1) Line 1 Line 2 Line … Layout Nr 2123 (with orderPosition 2) Line 1 Line … … 我正在使用我这样创建的NSFetchedResultsController: NSEntityDescription *entity =

在核心数据中,我有一对多关系-每个布局有许多行:

我试图通过分组到布局来显示表视图中的数据:

Layout Nr 2342 (with orderPosition 1)
    Line 1
    Line 2
    Line …
Layout Nr 2123 (with orderPosition 2)
    Line 1
    Line …
…
我正在使用我这样创建的
NSFetchedResultsController

NSEntityDescription *entity = [NSEntityDescription entityForName:@“Line” inManagedObjectContext:managedObjectContext];

NSSortDescriptor *orderPositionDescriptor = [[NSSortDescriptor alloc] initWithKey:@"layout.orderPosition" ascending:YES];

NSFetchRequest *fetchRequest = [[NSFetchRequest alloc] init];
[fetchRequest setSortDescriptors:@[orderPositionDescriptor]];
[fetchRequest setEntity:entity];

NSPredicate *predicate = [NSPredicate predicateWithFormat:[NSString stringWithFormat:@"section=='my_section'"]];
[fetchRequest setPredicate:predicate];

self.fetchController = [[NSFetchedResultsController alloc]
                                          initWithFetchRequest:fetchRequest
                                          managedObjectContext:managedObjectContext
                                          sectionNameKeyPath:@"layout.groupNumber"
                                          cacheName:nil];
所以我将fetch控制器设置为按布局groupNumber分组,还将排序描述符设置为按布局位置排序(每个布局都有自己的位置)。一旦将数据添加到核心数据,它就会显示在表视图中。在我尝试更改现有
NSFetchedResultsController
nsfetchedRequest
谓词之前,一切都很正常:

 NSPredicate *predicate = [NSPredicate predicateWithFormat:[NSString stringWithFormat:@"section=='%@'",section]];
 [self.fetchController.fetchRequest setPredicate:predicate]; 

我在谓词中设置了与已设置完全相同的部分,并且表中显示的
performFetch
之后的数据仍然相同,但是布局(表部分)会改变表中的位置,并且行显示在错误的布局中(基本上没有顺序)。我发现,如果我将第一个排序描述符设置为sectionNameKeyPath-“layout.groupNumber”,那么即使我更改了谓词,一切都会正常工作。但很明显,这并不是我想要达到的目的。有什么解决办法吗?任何帮助都将不胜感激

如果要在表视图中使用节,并对表视图数据进行排序,请务必了解第一个排序描述符必须与sectionNameKeyPath相同

因此,我建议您将代码更改为与此类似的内容

    NSEntityDescription *entity = [NSEntityDescription entityForName:@“Line” inManagedObjectContext:managedObjectContext];

//new line of code following...
    NSSortDescriptor *sectionNameDescriptor = [[NSSortDescriptor alloc] initWithKey:@"layout.groupNumber" ascending:YES];
    NSSortDescriptor *orderPositionDescriptor = [[NSSortDescriptor alloc] initWithKey:@"layout.orderPosition" ascending:YES];

    NSFetchRequest *fetchRequest = [[NSFetchRequest alloc] init];
//altered line of code following...
    [fetchRequest setSortDescriptors:@[sectionNameDescriptor, orderPositionDescriptor]];
    [fetchRequest setEntity:entity];

    NSPredicate *predicate = [NSPredicate predicateWithFormat:[NSString stringWithFormat:@"section=='my_section'"]];
    [fetchRequest setPredicate:predicate];

    self.fetchController = [[NSFetchedResultsController alloc]
                                      initWithFetchRequest:fetchRequest
                                      managedObjectContext:managedObjectContext
                                      sectionNameKeyPath:@"layout.groupNumber"
                                      cacheName:nil];
我读过这篇文章,但不记得在哪里。。。所以没有文档参考,很抱歉


这有帮助吗?

我找到了解决问题的方法。因为每个布局组号都有唯一的订单位置,所以解决方案不是按组号分组,而是按订单位置分组。在这种情况下,我可以指定orderPosition作为第一个(也是唯一一个)排序描述符。如果其他人有其他更好的解决方案,听到它还是很有趣的

NSEntityDescription *entity = [NSEntityDescription entityForName:@“Line” inManagedObjectContext:managedObjectContext];

NSSortDescriptor *orderPositionDescriptor = [[NSSortDescriptor alloc] initWithKey:@"layout.orderPosition" ascending:YES];

NSFetchRequest *fetchRequest = [[NSFetchRequest alloc] init];
[fetchRequest setSortDescriptors:@[orderPositionDescriptor]];
[fetchRequest setEntity:entity];

NSPredicate *predicate = [NSPredicate predicateWithFormat:[NSString stringWithFormat:@"section=='my_section'"]];
[fetchRequest setPredicate:predicate];

self.fetchController = [[NSFetchedResultsController alloc]
                                          initWithFetchRequest:fetchRequest
                                          managedObjectContext:managedObjectContext
                                          sectionNameKeyPath:@"layout.orderPosition"
                                          cacheName:nil];

感谢您的帮助,但正如我在问题中已经提到的:“如果我将第一个排序描述符设置为与sectionNameKeyPath-layout.groupNumber相同,那么即使我更改了谓词,一切都会工作”。如果我的第一个排序顺序描述符是groupNumber,那么即使orderPosition被设置为第二个排序描述符(数据将按组号排序),排序也将是错误的。啊,抱歉@sash没有仔细阅读。。。我再看一眼:)