NSFetchedResultsController objectAtIndex,objectAtIndexPath,indexPathForObject inconsistencies [英] NSFetchedResultsController objectAtIndex, objectAtIndexPath, indexPathForObject inconsistencies

查看:286
本文介绍了NSFetchedResultsController objectAtIndex,objectAtIndexPath,indexPathForObject inconsistencies的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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



将一行(对于一个SearchResult对象)插入到相应的表中。该对象似乎被正确插入到新表中。



这是发生错误的代码:

   - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath 
{
SearchResult * event;
NSLog(@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传递给方法:%@,indexPath);

SearchResult * result = [fetchedResultsController objectAtIndexPath:indexPath]; // ***这里是错误发生的地方***

[viewDelegate getDetails:result];
}

我在最后一行出现故障。日志如下所示:

 在fetchedResultsController中的节数:1 
在索引[0]处的对象:Cirles
indexPath对象在index [0] :( null)
对象在索引[1]:Begin
indexPath对象在索引[1] :( null)
对象在索引[2]:复制
indexPath索引处的对象[2] :( null)
索引处的对象[3]:未绑定
indexPath索引处的对象[3] :( null)
indexPath传递给方法:< NSIndexPath 0x64ddea0> 2 index [0,2]

执行NSLog语句后,使用 [fetchedResultsController objectAtIndexPath:indexPath]



***由于未捕获异常NSInternalInconsistencyException而终止应用程序,原因: '在索引0处的索引2处没有对象'



因此,总而言之,似乎存在正确数量的获取对象,有一个部分(0),我可以访问每一个一个方法,但不是由另一个。 indexPathForObject:总是返回(null)。



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






UPDATE



这是代码,实现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){
//配置第一个单元格来替换空列表指针,首先删除空行
[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){
//配置第一个单元格以显示我们有一个空列表
[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;

默认值:
break;
}

}

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

switch(type){
case NSFetchedResultsChangeInsert:
NSLog );

break;

case NSFetchedResultsChangeDelete:
NSLog(@收藏夹部分已删除);

break;

默认值:
break;
}

}





$ b b

UPDATE 2



我不知道是否重要,但是tableView是用这一行初始化的:

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






UPDATE 3



当我改变违规行如下,它工作正常。但我宁愿保持索引与它的表一样。

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




对我来说,它有助于初始化 NSFetchedResultsController

code>与 cacheName:nil


I have a fetched result with a single section. I am able to access the objects using [[fetchedResultsController fetchedObjects] objectAtIndex:index] for all of the objects. But it is failing when I use objectAtIndexPath like this: [fetchedResultsController objectAtIndexpath:indexPath]

An error occurs after I insert an row (for one of the SearchResult objects) into the corresponding table. The object appears to be inserted into the new table correctly. After I have visually confirmed this, I select one of the rows, and then the fun begins.

Here is the code where the error is occurring:

- (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];
}

I am having a failure in the last line. The log looks like this:

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]

After executing the NSLog statements, I get an exception at the last line using [fetchedResultsController objectAtIndexPath:indexPath]. It happens for other index values too, but they always look valid.

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

So, to summarize, there appear to be the right number of fetched objects, there is one section (0), I can access each one by one method, but not by the other. The indexPathForObject: is always returning (null).

Is this a bug or am I misunderstanding something?


UPDATE

Here is the code, implementing the NSFetchedResultsControllerDelegate protocol methods.

- (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;
    }

}


UPDATE 2

I don't know if it matters, but the tableView is initialized with this line:

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


UPDATE 3

When I change the offending line as follows, it works fine. But I would rather keep the indexing the same as it is with the table.

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

解决方案

I had the same problem now.

For me it helped to initialise NSFetchedResultsController with cacheName: nil.

这篇关于NSFetchedResultsController objectAtIndex,objectAtIndexPath,indexPathForObject inconsistencies的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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