NSFetchedResultsController:排序描述符和部分 [英] NSFetchedResultsController: sort descriptors and sections

查看:84
本文介绍了NSFetchedResultsController:排序描述符和部分的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个像这样的核心数据模型...

  [Country]< ---> [联盟]< ---> [匹配] 

我正在使用 NSFetchedResultsController 匹配项显示到 UITableView 中。



我已经做了一百万遍了,但是由于某些原因,这些节出现了问题,我无法弄清楚为什么。



我创建了如下的排序描述符所以...

  NSSortDescriptor * countrySD = [NSSortDescriptor sortDescriptorWithKey:@ league.country.name升序:是]; 
NSSortDescriptor * leagueSD = [NSSortDescriptor sortDescriptorWithKey:@ league.name升序:是];
NSSortDescriptor * dateSD = [NSSortDescriptor sortDescriptorWithKey:@ startDate升序:是];
request.sortDescriptors = @ [countrySD,LeagueSD,dateSD];

首先,我想检查一下我是否将它们按正确的顺序放置。首先应按 country.name 排序,然后按 league.name 排序,然后按 startDate



ie


  1. 阿尔巴尼亚中的任何内容都应先于西班牙中的任何内容。

  2. 国家/地区,联赛1 中的任何内容都应该先于联赛2 中的任何内容。

  3. 在单个联赛中,所有比赛均应以最早的顺序显示在 startDate 中。

然后我用这个...创建 NSFRC ...

  _fetchedResultsController = [[NSFetchedResultsController alloc] initWithFetchRequest:requestmanagedObjectContext:self.moc sectionNameKeyPath:@ league.leagueID cacheName:nil]; 

因此,应按与 league.leagueID不同的匹配项将表格分组code>值。



应该类似于...

 阿尔巴尼亚-联赛1 
12:00
13:00
阿尔巴尼亚-联赛2
09:00
14:00
法国-联赛1
09:00
西班牙-A联赛
08:00
12:00
西班牙-B联赛
09:00

虽然没有用。我在同一个联赛中获得多个头球。某些比赛出现在错误的标题下,等等...



我检查了出现在错误联赛中的比赛的值(NSLogged),实际上它们具有正确的联赛。因此,即使他们拥有西班牙-甲级联赛,它们也会出现在法国-甲级联赛下。 p>

有什么办法解决这个问题吗?

解决方案

使用的关键路径因为 sectionNameKeyPath 参数必须与在第一个排序描述符中使用的键
相同(或生成相同的相对顺序)。



(据我所知)没有办法使用两个或多个排序描述符将
的获取结果控制器的结果分为几部分。


I have a core data model like so...

[Country] <--->> [League] <--->> [Match]

And I'm using an NSFetchedResultsController to display the Matches into a UITableView.

I've done this a million times before but for some reason the sections are going wrong and I can't work out why.

I have created the sort descriptors like so...

NSSortDescriptor *countrySD = [NSSortDescriptor sortDescriptorWithKey:@"league.country.name" ascending:YES];
    NSSortDescriptor *leagueSD = [NSSortDescriptor sortDescriptorWithKey:@"league.name" ascending:YES];
    NSSortDescriptor *dateSD = [NSSortDescriptor sortDescriptorWithKey:@"startDate" ascending:YES];
    request.sortDescriptors = @[countrySD, leagueSD, dateSD];

First I want to check I have put these in the correct order. This should first sort by the country.name then sort by the league.name and then sort by the startDate.

i.e.

  1. Anything in Albania should come before anything in Spain.
  2. In a single country, anything in League 1 should come before anything in League 2.
  3. In a single league all the matches should be displayed in startDate order with the earliest first.

Then I'm creating the NSFRC with this...

_fetchedResultsController = [[NSFetchedResultsController alloc] initWithFetchRequest:request managedObjectContext:self.moc sectionNameKeyPath:@"league.leagueID" cacheName:nil];

So this should group the table by matches with different league.leagueID values.

It should be something like...

Albania - League 1
    12:00
    13:00
Albania - League 2
    09:00
    14:00
France - League 1
    09:00
Spain - A League
    08:00
    12:00
Spain - B League
    09:00

It isn't working though. I get multiple headers for the same league. Some of the matches appear under the wrong header etc...

I've checked the values (NSLogged) of the matches appearing under the wrong league and they actually have the correct league. So even though they have Spain - A League they are appearing under France - League A (for example).

Any idea how I can fix this?

解决方案

The key path used as sectionNameKeyPath argument must be the same key used in the first sort descriptor (or generate the same relative ordering).

There is (as far as I know) no way to use two or more sort descriptors for grouping the results of a fetched results controller into sections.

这篇关于NSFetchedResultsController:排序描述符和部分的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆