NSSortDescriptor with Comparator Changes Behavior on Fetch vs Initial Load

I'm getting really bizarre behavior with an NSSortDescriptor. Let's start with my code:

NSSortDescriptor *nameExists = [NSSortDescriptor sortDescriptorWithKey:@"firstName" ascending:YES comparator:^(id obj1, id obj2){
  if ( ([obj1 isEqualToString:@""] || [obj1 isKindOfClass:[NSNull class]] || obj1 == nil) && ![obj2 isEqualToString:@""]){
    return NSOrderedDescending;
  }
  else if ( ![obj1 isEqualToString:@""] && ([obj2 isEqualToString:@""] || [obj2 isKindOfClass:[NSNull class]] || obj2 == nil) ){
    return NSOrderedAscending;
  }
  return NSOrderedSame;
}];

This is the sort descriptor that I use to order the users in my database by those who have name fields and those who do not. So, if you had a user list of 20 people, and 5 people didn't have first names, but 15 did, the 15 with names would be sorted before those without. This is in start contrast to the usual behavior of sorting a string field as Ascending, which would display the entries with the blank fields before the non-blank fields.

When I first load the view, this sort descriptor reverts to the default behavior - with the blank fields before the full fields, as if my Comparator were not there.

However, if I pull new entries into Core Data, my NSFetchedResultsController loads the entries properly, and according to my comparator.

My gut told me that there is something wrong with the placement of my block. I have it in the init method of the view. However, I changed it to the loadView method and the behavior persisted.

Does anyone have any idea what is going on?

链接地址: http://www.djcxy.com/p/36176.html

上一篇: 将NSFetchedResultsController与属于实体的NSSet中的对象排序

下一篇: 带有比较器更改的NSSortDescriptor在获取与初始加载时的行为