3

我有一个 NSFetchRequest 正在获取一个名为“Player”的实体,我希望结果按以下顺序按 3 个属性排序:

  1. 名为“sendingoffOffence”的属性是否为 nil
  2. “team.homeMatch”实体中的关系是否为零
  3. 我希望“数字”属性中的字符串按升序排序。

基本上,我希望所有有红牌的球员(“sendingOffOffence”不是零)出现在列表的顶部,然后根据球员是否在主队排序,然后最后让进攻组中的一组球员按他们的球衣号码排序。

因此,我在获取请求中使用以下代码:

// Set the entity for the fetch request
NSEntityDescription *entity = [NSEntityDescription
                               entityForName:@"Player"
                               inManagedObjectContext:self.match.managedObjectContext];
[fetchRequest setEntity:entity];

NSPredicate *predicate = [NSPredicate predicateWithFormat:@"match == %@", self.match];
[fetchRequest setPredicate:predicate];

// Sort the results

// Sort according to whether the user has a red card / sendingOffOffence
NSSortDescriptor *hasRedCard = [[NSSortDescriptor alloc] initWithKey:@"sendingoffOffence" ascending:NO];

// Sort according to whether the user is on the home team or not
NSSortDescriptor *isHomeTeam = [[NSSortDescriptor alloc] initWithKey:@"team.homeMatch" ascending:NO];

// Sort according to the player's jersey numbers
NSSortDescriptor *number     = [[NSSortDescriptor alloc] initWithKey:@"number" ascending:YES selector:@selector(localizedStandardCompare:)];

[fetchRequest setSortDescriptors:@[hasRedCard, isHomeTeam, number]];

// Set the amount of records to retrieve from the database
[fetchRequest setFetchBatchSize:20];

NSFetchedResultsController *theFetchedResultsController =
    [[NSFetchedResultsController alloc]
     initWithFetchRequest:fetchRequest
     managedObjectContext:self.match.managedObjectContext
     sectionNameKeyPath:nil
     cacheName:@"MatchCache"]

但是,当我执行上面的代码时,我得到以下结果:

红牌排序 黄牌排序

这个排序顺序是错误的,因为我希望以下顺序出现在红牌部分:

  • 11 - 主页
  • 12 - 主页
  • 0 - 离开
  • 11 - 离开
  • 21 - 离开

在黄牌区:

  • 1 - 主页
  • 2 - 主页
  • 99 - 主页
  • 1 - 离开
  • 31 - 离开

看起来黄牌部分排序正确,但红牌部分表现出非常奇怪的行为,使其看起来根本没有得到排序。

我对为什么红牌部分无法正确排序感到很困惑——有什么想法吗?我应该只在内存中对这些对象进行排序,而不是依靠核心数据来获得我喜欢的顺序吗?

请注意,这是一个带有 SQL 支持的持久性存储的核心数据应用程序。

更新

以下是核心数据在我的 fetch 中使用的 SQL 语句:

CoreData: annotation: fetch using NSSQLiteStatement <0x11c77cd0> on entity 'SPlayer' with 
sql text 'SELECT t0.Z_ENT, t0.Z_PK FROM ZFSMANAGEDOBJECT t0 LEFT OUTER JOIN ZSTEAM t1 ON 
t0.ZTEAM = t1.Z_PK WHERE ( t0.ZMATCH1 = ? AND  t0.Z_ENT = ?) ORDER BY 
t0.ZSENDINGOFFOFFENCE DESC, t1.ZHOMEMATCH DESC, t0.ZNUMBER COLLATE NSCollateFinderlike '

4

2 回答 2

3

这是未正确应用的行(对于 RED 卡片盒):

NSSortDescriptor *isHomeTeam = [[NSSortDescriptor alloc] initWithKey:@"team.homeMatch" ascending:NO];

如果没有看到您的数据模型和一些示例数据,很难说为什么这不会正确排序。我会假设这将首先放置 NIL 值,然后是适当的值(因此排序升序 NO 会做它在黄色卡片盒中所做的事情)。

如果我处于这种情况,我会检查我对排序顺序和这些 team.homeMatch 属性的假设。做一个测试,这是唯一的排序描述。

然后专注于与红/黄卡状况不同的地方。

于 2013-06-27T12:23:33.240 回答
1

我们没有像这样检查它的 NSFetchedResultsController 。

NSEntityDescription *entity = [NSEntityDescription
                               entityForName:@"Player"
                               inManagedObjectContext:self.match.managedObjectContext];
[fetchRequest setEntity:entity];

NSPredicate *predicate = [NSPredicate predicateWithFormat:@"match == %@", self.match];
[fetchRequest setPredicate:predicate];

// Sort the results

// Sort according to whether the user has a red card / sendingOffOffence
NSSortDescriptor *hasRedCard = [[NSSortDescriptor alloc] initWithKey:@"sendingoffOffence" ascending:NO];

// Sort according to whether the user is on the home team or not
NSSortDescriptor *isHomeTeam = [[NSSortDescriptor alloc] initWithKey:@"team.homeMatch" ascending:NO];


[fetchRequest setSortDescriptors:@[hasRedCard, isHomeTeam]];

// Set the amount of records to retrieve from the database
[fetchRequest setFetchBatchSize:20];

NSArray *resultArray = [self.match.managedObjectContext executeFetchRequest:request error:&error];


NSSortDescriptor *number     = [[NSSortDescriptor alloc] initWithKey:@"number" ascending:YES selector:@selector(localizedStandardCompare:)];
    NSArray *sortedArray = [resultArray sortedArrayUsingDescriptors:[NSArray arrayWithObjects:number, nil]];
于 2013-06-27T12:26:37.573 回答