CoreDataBooks 问题断言失败。

Posted

技术标签:

【中文标题】CoreDataBooks 问题断言失败。【英文标题】:CoreDataBooks Issue Assertion failure. 【发布时间】:2014-03-22 19:59:02 【问题描述】:

所以我尝试将Apple的CoreDataBooks Demo复制到我的项目中,几乎相同。

但是当我尝试添加项目时出现以下错误:

2014-03-22 20:21:15.108 [7592:60b] *** Assertion failure in -[UITableView _endCellAnimationsWithContext:], /SourceCache/UIKit/UIKit-2935.137/UITableView.m:1176
2014-03-22 20:21:15.111 [7592:60b] CoreData: error: Serious application error.  An exception was caught from the delegate of NSFetchedResultsController during a call to -controllerDidChangeContent:.  attempt to insert row 0 into section 0, but there are only 0 rows in section 0 after the update with userInfo (null)

TableView-代码:

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView

    return 1;


- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section

    return self.cart.count;


- (void)configureCell:(UITableViewCell *)cell atIndexPath:(NSIndexPath *)indexPath 

    // Configure the cell to show the book's title
    Cart *cart = [self.fetchedResultsController objectAtIndexPath:indexPath];
    cell.textLabel.text = cart.cartProductName;


- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath 

    static NSString *CellIdentifier = @"Cell";
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];

    // Configure the cell.
    [self configureCell:cell atIndexPath:indexPath];
    return cell;



- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath

    // Return NO if you do not want the specified item to be editable.
    return YES;


- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath 

    if (editingStyle == UITableViewCellEditingStyleDelete) 

        // Delete the managed object.
        NSManagedObjectContext *context = [self.fetchedResultsController managedObjectContext];
        [context deleteObject:[self.fetchedResultsController objectAtIndexPath:indexPath]];

        NSError *error;
        if (![context save:&error]) 
            /*
             Replace this implementation with code to handle the error appropriately.

             abort() causes the application to generate a crash log and terminate. You should not use this function in a shipping application, although it may be useful during development.
             */
            NSLog(@"Unresolved error %@, %@", error, [error userInfo]);
            abort();
        
    


- (BOOL)tableView:(UITableView *)tableView canMoveRowAtIndexPath:(NSIndexPath *)indexPath 

    // The table view should not be re-orderable.
    return NO;


#pragma mark - Fetched results controller

/*
 Returns the fetched results controller. Creates and configures the controller if necessary.
 */
- (NSFetchedResultsController *)fetchedResultsController 

    if (_fetchedResultsController != nil) 
        return _fetchedResultsController;
    

    // Create and configure a fetch request with the Book entity.
    NSFetchRequest *fetchRequest = [[NSFetchRequest alloc] init];
    NSEntityDescription *entity = [NSEntityDescription entityForName:@"Cart" inManagedObjectContext:self.managedObjectContext];
    [fetchRequest setEntity:entity];

    // Create the sort descriptors array.
    NSSortDescriptor *productDescriptor = [[NSSortDescriptor alloc] initWithKey:@"cartProductName" ascending:YES];
    NSArray *sortDescriptors = @[productDescriptor];
    [fetchRequest setSortDescriptors:sortDescriptors];

    // Create and initialize the fetch results controller.
    _fetchedResultsController = [[NSFetchedResultsController alloc] initWithFetchRequest:fetchRequest managedObjectContext:self.managedObjectContext sectionNameKeyPath:@"cartProductName" cacheName:@"Root"];
    _fetchedResultsController.delegate = self;

    return _fetchedResultsController;



/*
 NSFetchedResultsController delegate methods to respond to additions, removals and so on.
 */
- (void)controllerWillChangeContent:(NSFetchedResultsController *)controller 

    // The fetch controller is about to start sending change notifications, so prepare the table view for updates.
    [self.tableView beginUpdates];



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

    UITableView *tableView = self.tableView;

    switch(type) 

        case NSFetchedResultsChangeInsert:
            [tableView insertRowsAtIndexPaths:@[newIndexPath] withRowAnimation:UITableViewRowAnimationAutomatic];
            break;

        case NSFetchedResultsChangeDelete:
            [tableView deleteRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationAutomatic];
            break;

        case NSFetchedResultsChangeUpdate:
            [self configureCell:[tableView cellForRowAtIndexPath:indexPath] atIndexPath:indexPath];
            break;

        case NSFetchedResultsChangeMove:
            [tableView deleteRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationAutomatic];
            [tableView insertRowsAtIndexPaths:@[newIndexPath] withRowAnimation:UITableViewRowAnimationAutomatic];
            break;
    



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

    switch(type) 

        case NSFetchedResultsChangeInsert:
            [self.tableView insertSections:[NSIndexSet indexSetWithIndex:sectionIndex] withRowAnimation:UITableViewRowAnimationAutomatic];
            break;

        case NSFetchedResultsChangeDelete:
            [self.tableView deleteSections:[NSIndexSet indexSetWithIndex:sectionIndex] withRowAnimation:UITableViewRowAnimationAutomatic];
            break;
    


- (void)controllerDidChangeContent:(NSFetchedResultsController *)controller 

    [self.tableView endUpdates];


问题是,我对编程很陌生,并搜索了错误,但我得到的结果似乎不一样。

【问题讨论】:

【参考方案1】:

您的前两个方法看起来不对(尤其是 numberOfRowsInSection 方法)。 您应该使用“标准实现”

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView

    return [[self.fetchedResultsController sections] count];


- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section

    id <NSFetchedResultsSectionInfo> sectionInfo = [self.fetchedResultsController sections][section];
    return [sectionInfo numberOfObjects];

发生错误是因为self.cart.count(无论是什么)不反映 插入项目后正确数量的对象。

【讨论】:

以上是关于CoreDataBooks 问题断言失败。的主要内容,如果未能解决你的问题,请参考以下文章

CoreDataBooks 示例 - 从索引 1 开始单元格?

OpenCV:数据类型断言因 split() 函数而失败

Android NDK:断言失败:未定义 LOCAL_MAKEFILE

我正在为我的测试项目使用 MStest (.net core),有没有办法在失败时继续执行测试?

破坏 Glib::RefPtr 会导致 GTK 3 核心中的断言失败

Android NDK:断言失败:LOCAL_MAKEFILE未定义