NSFetchedresultscontroller与tableview插入新的部分崩溃

时间:2014-05-30 16:36:51

标签: ios objective-c uitableview nsfetchedresultscontroller

我正在处理消息应用以及我保存在核心数据中的所有消息。我使用NSFetchedResultsController和tableView在屏幕上显示它们。我的nsfetchedResultsController中的部分是一周的日子。我的意思是一个部分是例如今天另一部分例如是昨天。当我想添加应该插入新部分的新消息时,当我尝试将新对象插入到在tableview中生成新部分的核心数据时,我遇到了崩溃。这是我得到的日志:

  

无效更新:第0节中的行数无效。更新后的现有部分中包含的行数(12)必须等于更新前该部分中包含的行数(8),或者减去从该部分插入或删除的行数(插入1个,删除1个),加上或减去移入或移出该部分的行数(0移入,0移出)

此处是NSFETCHRESULTSCONTROLLER DELEGATE的代码

- (void)dataLoaderDidChangeContent:(GISingleConversationDataLoader *)controller {
    [self.tableView endUpdates];

    if (_shouldScrollToBottomAfterUpdate) {
        [self scrollToBottomAnimated:NO];
    }
}

- (void)dataLoaderWillChangeContent:(GISingleConversationDataLoader *)controller {
    [self.tableView beginUpdates];
}

- (void)dataLoader:(GISingleConversationDataLoader *)controller didChangeObject:(id)anObject atIndexPath:(NSIndexPath *)indexPath forChangeType:(NSFetchedResultsChangeType)type newIndexPath:(NSIndexPath *)newIndexPath {
    UITableView *tableView = self.tableView;

    switch(type) {

        case NSFetchedResultsChangeInsert:
            if ([self.tableView numberOfSections]) {
                [tableView insertRowsAtIndexPaths:[NSArray arrayWithObject:newIndexPath]
                                 withRowAnimation:UITableViewRowAnimationBottom];
            } else {
                [tableView insertSections:[NSIndexSet indexSetWithIndex:0] withRowAnimation:UITableViewRowAnimationNone];
            }
            _shouldScrollToBottomAfterUpdate = YES;
            break;

        case NSFetchedResultsChangeDelete:
            [tableView deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPath]
                             withRowAnimation:UITableViewRowAnimationFade];
            break;

        case NSFetchedResultsChangeUpdate:
            [tableView reloadRowsAtIndexPaths:[NSArray arrayWithObject:indexPath]
                                 withRowAnimation:UITableViewRowAnimationNone];
            break;

        case NSFetchedResultsChangeMove:
            [tableView deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPath]
                             withRowAnimation:UITableViewRowAnimationNone];
            [tableView insertRowsAtIndexPaths:[NSArray arrayWithObject:newIndexPath]
                             withRowAnimation:UITableViewRowAnimationNone];
            break;
    }
}

数据来源:

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return [self.dataProvider.fetchedResultsController.sections count];
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    if ([[self.dataProvider.fetchedResultsController sections] count] < 1) {
        return 0;
    }
    id <NSFetchedResultsSectionInfo> sectionInfo = [[self.dataProvider.fetchedResultsController sections] objectAtIndex:section];
    return [sectionInfo numberOfObjects];
}

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

    GICDChatMessage *message = [self _messageAtIndexPath:indexPath];

    GIChatMessageType type = [message.type integerValue];

    NSString *kMessageTypeKey = [message isOutgoing] ? kOutgoingMessageKey : kIncomingMessageKey;
    NSString *CellIdentifier;  CellIdentifier = self.cellIdentifiers[@(type)][kMessageTypeKey];

    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier forIndexPath:indexPath];
    self.configureCellBlock(cell, message, indexPath);

    [cell setSelectionStyle:UITableViewCellSelectionStyleNone];
    [cell setBackgroundColor:tableView.backgroundColor];

    return cell;
}

我创建NSFetchedResultsController并在数据加载器中管理他。我创建它以使我的viewController更轻。

这是我的数据加载器实现:

- (id)initWithConversation:(GIConversation *)conversation delegate:(id<GISingleConversationDataLoaderProtocol>)delegate {
    self = [super init];
    if (self) {
        _conversation = conversation;
        _delegate = delegate;
        [self resultsController];
        [_fetchedResultsController setDelegate:self];
        [self performFetch];
    }
    return self;
}

- (NSPredicate *)predicate {
    return [NSPredicate predicateWithFormat:@"conversationJid == %@", _conversation.jid];
}

- (NSString *)entityName {
    return @"GICDChatMessage";
}

- (NSArray *)sortDescriptors {
    return @[[NSSortDescriptor sortDescriptorWithKey:@"receivedAt" ascending:YES]];
}

- (NSFetchedResultsController*)resultsController {
    if (_fetchedResultsController == nil) {
        _fetchedResultsController = [[NSFetchedResultsController alloc] initWithFetchRequest:[self fetchRequest] managedObjectContext:[self context] sectionNameKeyPath:@"sectionNameKeyPath" cacheName:nil];
    }
    return _fetchedResultsController;
}

-(void)performFetch {
    if (_fetchedResultsController) {
        [_fetchedResultsController performFetch:nil];
        [_delegate dataLoaderDidChangeContent:self];
    }
}


- (NSFetchRequest*)fetchRequest {

    NSFetchRequest *result = [NSFetchRequest fetchRequestWithEntityName:[self entityName]];

    NSPredicate *mainPredicate = [self predicate];
    result.predicate = mainPredicate;
    result.sortDescriptors = [self sortDescriptors];

    return result;
}
- (NSManagedObjectContext *)context {
    return [[GICoreDataManager sharedInstance] mainContext];
}

#pragma mark - fetch change

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

    [_delegate dataLoader:self
          didChangeObject:anObject
              atIndexPath:indexPath
            forChangeType:type
             newIndexPath:newIndexPath];
}

- (void)controllerWillChangeContent:(NSFetchedResultsController *)controller {
    [_delegate dataLoaderWillChangeContent:self];
}

- (void)controllerDidChangeContent:(NSFetchedResultsController *)controller {
    [_delegate dataLoaderDidChangeContent:self];
}

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

    [_delegate dataLoader:self didChangeSection:sectionInfo atIndex:sectionIndex forChangeType:type];
}

> sectionNameKeyPath是一个在我的模型类类别中定义的键,它看起来是:

- (NSString *)sectionNameKeyPath {

    NSTimeInterval timeInterval = [self.receivedAt timeIntervalSinceNow];
    NSString *string = [[GICDChatMessage timeIntervalFormatter] stringForTimeInterval:roundf(timeInterval / oneDayTimeInterval) * oneDayTimeInterval];

    if ([string isEqualToString:NSLocalizedString(@"just now", nil)]) {
        string = NSLocalizedString(@"Today", nil);
    }

    return string;
}

它只是为每天的消息排序。

你知道是什么导致了这个问题或者我做错了什么吗?

我的英语不好,所以如果你不明白我的意思,请问我,我会尝试更清楚地解释。

3 个答案:

答案 0 :(得分:3)

您需要实现didChangeSection:

- (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:UITableViewRowAnimationFade];
            break;

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

编辑您还需要删除管理部分的代码部分:

   if ([self.tableView numberOfSections]) {
        [tableView insertRowsAtIndexPaths:[NSArray arrayWithObject:newIndexPath]
                         withRowAnimation:UITableViewRowAnimationBottom];
    } else {
        [tableView insertSections:[NSIndexSet indexSetWithIndex:0] withRowAnimation:UITableViewRowAnimationNone];
    }

答案 1 :(得分:0)

您尚未实施NSFetchedResultsControllerDelegate协议的任何方法。相反,您已经编写了具有相似名称的自定义方法。例如,这里介绍了您发布的方法:

- (void)dataLoaderWillChangeContent:(GISingleConversationDataLoader *)controller {
    [self.tableView beginUpdates];
}

虽然上述方法的名称与NSFetchedResultsControllerDelegate协议的方法类似,但它不是同一种方法,因此永远不会被提取的结果控制器调用。如果您希望获取的结果控制器调用它,那么您需要实现该方法的声明:

- (void)controllerWillChangeContent:(NSFetchedResultsController *)controller;

同样,您应该更改代码的该部分中其他方法的名称和签名,以匹配Core Data提供的实际API。

答案 2 :(得分:0)

汇总更新

然后在controllerDidChangeContent中,您需要处理所有收到的更新并转发到UITableView

此代码非常适合我:

class SomeVC: UITableViewController {

        /* ... */

// Add properties for each expected update type

    var insertedSectionIndexes = NSMutableIndexSet()
    var deletedSectionIndexes  = NSMutableIndexSet()

    var deletedRowIndexPaths:  Set<NSIndexPath> = []
    var insertedRowIndexPaths: Set<NSIndexPath> = []
    var updatedRowIndexPaths:  Set<NSIndexPath> = []
}



extension SomeVC: NSFetchedResultsControllerDelegate {

    func controllerWillChangeContent (controller: NSFetchedResultsController) {}

    func controller(controller: NSFetchedResultsController,
        didChangeObject anObject: NSManagedObject,
        atIndexPath indexPath: NSIndexPath?,
        forChangeType type: NSFetchedResultsChangeType,
        newIndexPath: NSIndexPath?) {
            switch type {
            case .Insert: insertedRowIndexPaths.insert(indexPath!)
            case .Update: updatedRowIndexPaths.insert(indexPath!)
            case .Move:
                if newIndexPath?.section != indexPath?.section {
                    insertedRowIndexPaths.insert(newIndexPath!)
                    deletedRowIndexPaths.insert(indexPath!)
                }
            case .Delete: deletedRowIndexPaths.insert(indexPath!)
            }
    }

    func controller(controller: NSFetchedResultsController,
        didChangeSection sectionInfo: NSFetchedResultsSectionInfo,
        atIndex sectionIndex: Int,
        forChangeType type: NSFetchedResultsChangeType) {
        switch type {
        case .Insert: insertedSectionIndexes.addIndex(sectionIndex)
        case .Delete: deletedSectionIndexes.addIndex(sectionIndex)
        default:()
        }
    }

    func controllerDidChangeContent (controller: NSFetchedResultsController) {

        tableView?.beginUpdates()

        tableView?.deleteSections(deletedSectionIndexes, withRowAnimation: .None)
        tableView?.insertSections(insertedSectionIndexes, withRowAnimation: .None)

        updatedRowIndexPaths.subtractInPlace(deletedRowIndexPaths)
        updatedRowIndexPaths.subtractInPlace(insertedRowIndexPaths)

        tableView?.deleteRowsAtIndexPaths(Array(deletedRowIndexPaths), withRowAnimation: .Automatic)
        tableView?.insertRowsAtIndexPaths(Array(insertedRowIndexPaths), withRowAnimation: .Automatic)
        tableView?.reloadRowsAtIndexPaths(Array(updatedRowIndexPaths), withRowAnimation: .Automatic)

        tableView?.endUpdates()
        // Do not forget to clean up!
        insertedSectionIndexes.removeAllIndexes()
        deletedSectionIndexes.removeAllIndexes()
        deletedRowIndexPaths = []
        insertedRowIndexPaths = []
        updatedRowIndexPaths = []
    }

}

注意:来自Xcode 7 beta 2的代码,某些方法可能被称为不同