【问题标题】:NSFetchedResultsController objectAtIndex, objectAtIndexPath, indexPathForObject inconsistenciesNSFetchedResultsController objectAtIndex、objectAtIndexPath、indexPathForObject 不一致
【发布时间】:2011-12-22 12:57:10
【问题描述】:

我有一个单独的部分的提取结果。我可以使用[[fetchedResultsController fetchedObjects] objectAtIndex:index] 访问所有对象的对象。但是当我像这样使用 objectAtIndexPath 时它失败了:[fetchedResultsController objectAtIndexpath:indexPath]

在我将一行(针对其中一个 SearchResult 对象)插入相应的表后发生错误。该对象似乎已正确插入到新表中。在我直观地确认了这一点后,我选择了其中一行,然后开始有趣。

这是发生错误的代码:

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    SearchResult *event;
    NSLog(@"Number of sections in fetchedResultsController: %d", [[fetchedResultsController sections] count]);
    for (NSUInteger i=0; i<[[fetchedResultsController fetchedObjects] count]; i++) {
        event = (SearchResult*)[[fetchedResultsController fetchedObjects] objectAtIndex:i];
        NSLog(@"object at index[%d]: %@", i, event.title );
        NSLog(@"indexPath for object at index[%d]:%@", i, [fetchedResultsController indexPathForObject:event]);
    }

    NSLog(@"indexPath passed to method: %@", indexPath);

    SearchResult *result = [fetchedResultsController objectAtIndexPath:indexPath];  // *** here is where the error occurs ***

    [viewDelegate getDetails:result];
}

我在最后一行失败了。日志如下所示:

Number of sections in fetchedResultsController: 1
object at index[0]: Cirles
indexPath for object at index[0]:(null)
object at index[1]: Begin
indexPath for object at index[1]:(null)
object at index[2]: Copy
indexPath for object at index[2]:(null)
object at index[3]: Unbound
indexPath for object at index[3]:(null)
indexPath passed to method: <NSIndexPath 0x64ddea0> 2 indexes [0, 2]

执行 NSLog 语句后,我在最后一行使用 [fetchedResultsController objectAtIndexPath:indexPath] 得到异常。其他索引值也会发生这种情况,但它们看起来总是有效的。

*** Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: 'no object at index 2 in section at index 0'

所以,总而言之,似乎有正确数量的提取对象,有一个部分 (0),我可以一个一个地访问每个方法,但不能通过另一个方法。 indexPathForObject: 总是返回 (null)。

这是一个错误还是我误解了什么?


更新

这是实现 NSFetchedResultsControllerDelegate 协议方法的代码。

- (void) controllerWillChangeContent:(NSFetchedResultsController *)controller {
    NSLog(@"Favorites controllerWillChangeContent");
    [self.tableView beginUpdates];
}

- (void)controllerDidChangeContent:(NSFetchedResultsController *)controller 
{
    NSLog(@"Favorites Table controllerDidChangeContent");
    [self.tableView endUpdates];
}

- (void)controller:(NSFetchedResultsController *)controller 
   didChangeObject:(id)anObject 
       atIndexPath:(NSIndexPath *)indexPath 
     forChangeType:(NSFetchedResultsChangeType)type 
      newIndexPath:(NSIndexPath *)newIndexPath {

    NSLog(@"Favorites Changed Object");

    switch (type) {
        case NSFetchedResultsChangeInsert:
            NSLog(@"--- Favorite was inserted");
            if ([[self.fetchedResultsController fetchedObjects] count] == 1) {
                // configure first cell to replace the empty list indicator by first deleting the "empty" row
                [self.tableView deleteRowsAtIndexPaths:[NSArray arrayWithObject:newIndexPath] withRowAnimation:UITableViewRowAnimationNone];
            }

            [self.tableView insertRowsAtIndexPaths:[NSArray arrayWithObject:newIndexPath] withRowAnimation:UITableViewRowAnimationNone];

            break;

        case NSFetchedResultsChangeDelete:
            NSLog(@"--- Favorite was deleted");

            [self.tableView deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationNone];
            if ([[self.fetchedResultsController fetchedObjects] count] == 0) {
                // configure first cell to show that we have an empty list
                [self.tableView insertRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationNone];
            }

            break;

        case NSFetchedResultsChangeMove:
            NSLog(@"--- Favorite was moved");

            break;

        case NSFetchedResultsChangeUpdate:
            NSLog(@"--- Favorite was updated");
            [self configureCell:(ResultCell*)[tableView cellForRowAtIndexPath:indexPath] atIndexPath:indexPath];

            break;

        default:
            break;
    }

}

- (void)controller:(NSFetchedResultsController *)controller
  didChangeSection:(id <NSFetchedResultsSectionInfo>)sectionInfo 
           atIndex:(NSUInteger)sectionIndex 
     forChangeType:(NSFetchedResultsChangeType)type {

    switch (type) {
        case NSFetchedResultsChangeInsert:
            NSLog(@"Favorites Section Added");

            break;

        case NSFetchedResultsChangeDelete:
            NSLog(@"Favorites Section Deleted");

            break;

        default:
            break;
    }

}

更新 2

不知道有没有关系,但是tableView是用这一行初始化的:

    tableView = [[UITableView alloc] initWithFrame:CGRectMake(...) style:UITableViewStyleGrouped];

更新 3

当我如下更改违规行时,它可以正常工作。但我宁愿保持索引与表相同。

//SearchResult *result = [self.fetchedResultsController objectAtIndexPath:indexPath];
SearchResult *result = [[self.fetchedResultsController fetchedObjects] objectAtIndex:[indexPath indexAtPosition:1]]

【问题讨论】:

  • 你是如何插入对象的?您可以发布将对象添加到核心数据存储并将行添加到 tableView 的代码吗?
  • 您是在向 Core Data Store 添加内容还是只是向 tableView 添加一行?
  • 是的,我在进行更改时会保存到托管对象上下文。这就是触发对 NSFetchedResultsControllerDelegate 协议方法的调用的原因。
  • 如果您有答案,则不应将答案插入问题中,而应将其插入答案中然后选择。
  • 嗨@Jim 我知道这是一篇很老的帖子,但你有没有找到比致电[[self.fetchedResultsController fetchedObjects] objectAtIndex:...] 更好的解决方案?

标签: objective-c core-data uitableview nsfetchedresultscontroller


【解决方案1】:

我现在遇到了同样的问题。

对我来说,用cacheName: nil 初始化NSFetchedResultsController 很有帮助。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-03-14
    • 2018-02-08
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多