5

我收到以下错误消息:

CoreData: error: (NSFetchedResultsController) The fetched object at index 5 has an out of order section name 'James. Objects must be sorted by section name'
Unresolved search error Error Domain=NSCocoaErrorDomain Code=134060 "The operation couldn’t be completed. (Cocoa error 134060.)" UserInfo=0xaa07530 {reason=The fetched object at index 5 has an out of order section name 'James. Objects must be sorted by section name'}, {
    reason = "The fetched object at index 5 has an out of order section name 'James. Objects must be sorted by section name'";
}

我已经经历了无数关于 SO 的答案,这些答案大多最终指向使用不区分大小写的比较作为答案,但是正如您在下面看到的那样,我的 fetch 请求已经设置为执行此操作:

<NSFetchRequest: 0xc082bd0> (entity: InviteeModel; predicate: (eventId == 148 AND typeOf != "InviteeTypeOfServerContact"); sortDescriptors: ((
    "(lastName, ascending, caseInsensitiveCompare:)",
    "(firstName, ascending, caseInsensitiveCompare:)"
)); batch size: 1000; type: NSManagedObjectResultType; )

我似乎已将其范围缩小到我有两个姓氏相同的受邀者(即亨利·詹姆斯和亨利·詹姆斯)的情况,然后我收到上面的错误,说詹姆斯出了问题。如果我将两个姓氏都更改为詹姆斯或詹姆斯,那么它会起作用吗?!?!?

以下是创建获取请求的代码:

NSFetchRequest *fetchRequest = [self buildFetchRequest];

// We have to reset the fetched results controller if the sort changes.
// Because otherwise the scrolling index will be inaccurate.
if (self.fetchedResultsController) {
    if (![self.fetchedResultsController.sectionNameKeyPath isEqualToString:self.sortBy]) {
        self.fetchedResultsController = nil;
    }
}


if (!self.fetchedResultsController) {

    NSManagedObjectContext *context = self.event.managedObjectContext;

    NSString *sectionNameKeyPath = nil;
    if (self.sortBy == DefaultSortBy) {

        sectionNameKeyPath = @"sectionChar";
    }
    else {

        sectionNameKeyPath = self.sortBy;
        if ([self.sortBy isEqualToString:@"rsvpState"] || [self.sortBy isEqualToString:@"checkedIn"]) {
            sectionNameKeyPath = @"lastName";
        }
    }

    NSFetchedResultsController *fetchedResultsController = [[NSFetchedResultsController alloc] initWithFetchRequest:fetchRequest 
                                                                                               managedObjectContext:context 
                                                                                                 sectionNameKeyPath:sectionNameKeyPath
                                                                                                          cacheName:nil];
    fetchedResultsController.delegate = self;
    self.fetchedResultsController = fetchedResultsController;

这是来自 buildFetchRequest 的一个片段,它添加了 sortDescriptors:

        NSSortDescriptor *sortDescriptor = [[NSSortDescriptor alloc] initWithKey:self.sortBy
                                                                       ascending:self.shouldSortAscending
                                                                        selector:@selector(caseInsensitiveCompare:)];

        // Add the default sorts (last, first).
        NSSortDescriptor *lastSort = nil;
        NSSortDescriptor *firstSort = nil;
        if (![self.sortBy isEqualToString:@"lastName"]) {
            lastSort = [NSSortDescriptor sortDescriptorWithKey:@"lastName"
                                                     ascending:YES
                                                      selector:@selector(caseInsensitiveCompare:)];
        }
        if (![self.sortBy isEqualToString:@"firstName"]) {
            firstSort = [NSSortDescriptor sortDescriptorWithKey:@"firstName"
                                                      ascending:YES
                                                       selector:@selector(caseInsensitiveCompare:)];
        }

        NSArray *sorts = nil;

        if (lastSort && firstSort) {
            sorts = [[NSArray alloc] initWithObjects:sortDescriptor, lastSort, firstSort, nil];
        }
        else if (lastSort) {
            sorts = [[NSArray alloc] initWithObjects:sortDescriptor, lastSort, nil];
        }
        else {
            sorts = [[NSArray alloc] initWithObjects:sortDescriptor, firstSort, nil];
        }

        [fetchRequest setSortDescriptors:sorts];

任何想法,这让我发疯。

4

2 回答 2

3

您正在设置与第一个排序描述符键不同的提取结果控制器部分名称键路径。

   NSSortDescriptor *sortDescriptor = [[NSSortDescriptor alloc] 
           initWithKey:self.sortBy 
           ascending:self.shouldSortAscending
           selector:@selector(caseInsensitiveCompare:)];

关键是self.sortBy

if ([self.sortBy isEqualToString:@"rsvpState"] || 
    [self.sortBy isEqualToString:@"checkedIn"]) {
       sectionNameKeyPath = @"lastName";
}

关键不是 self.sortBy

这应该会导致您看到的错误。

于 2013-08-21T12:55:00.690 回答
0

那么,关键问题是哪个属性正在确定 sectionNameKeyPath 以及该属性是否已用作排序描述符数组中的第一个排序描述符。

我想这是检查你是否有这样的问题的第一件事。

于 2014-04-22T03:07:54.843 回答