NSFetchedResultsController crashes on objectAtIndexPath?

后端 未结 2 1367
自闭症患者
自闭症患者 2021-01-28 22:14

I\'m getting a crash when trying to access an object in NSFetchedResultsController.

2013-11-10 15:15:06.568 Social[11503:70b] CoreData: error: Serio         


        
相关标签:
2条回答
  • 2021-01-28 22:26

    One reason for crash on below line could be that you are updating self.resultContrlller after you have used it for retuning the number of rows of your table section. Make sure that if you are constantly updating your self.resultContrlller object then you take a copy of it and then use for your table drawing.

    // This code does not prevent crash. Changing your regular code to modern objective-C way
    Post *post = [self.resultContrlller fetchedObjects][indexPath.row];
    

    The data source should not change while table is reloading itself. You should use a copy of fetchedObjects instead to load the table. So, evey time, before you reload your table, take a copy [[self.resultContrlller fetchedObjects] copy] and the use it for table drawing. Your main source can then keep on changing. And after table reload is done with copy you may want to reload it it again if there was a change in the data. Such crashes happens when your data source changes faster than table reloads.

    0 讨论(0)
  • 2021-01-28 22:39

    In one of my NSManagedObject subclasses I had the follwiing code which was causing the issue. NSSets are automatically initialized on NSManagedObejcts and there is no need to initialize them.

    - (void)awakeFromFetch
    {
        if (!self.comments)
            self.comments = [NSMutableSet set];
    
        if (!self.medias)
            self.medias = [NSMutableSet set];
    }
    

    Another problem was that during insert indexPath is null, I had to use newIndexPath instead

    case NSFetchedResultsChangeInsert:
        [self.tableView insertRowsAtIndexPaths:@[newIndexPath]
                              withRowAnimation:UITableViewRowAnimationAutomatic];
        break;
    
    0 讨论(0)
提交回复
热议问题