ios - NSFetchedresultscontroller 与 tableview 插入新部分崩溃

标签 ios objective-c uitableview nsfetchedresultscontroller

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

Invalid update: invalid number of rows in section 0. The number of rows contained in an existing section after the update (12) must be equal to the number of rows contained in that section before the update (8), plus or minus the number of rows inserted or deleted from that section (1 inserted, 1 deleted) and plus or minus the number of rows moved into or out of that section (0 moved in, 0 moved out)

这是我的 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;
}

它只是对每一天的消息进行排序。

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

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

最佳答案

你需要实现 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];
    }

关于ios - NSFetchedresultscontroller 与 tableview 插入新部分崩溃,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23959426/

相关文章:

ios - 翻转图像以显示背面的 iOS

iOS 可用蓝牙设备列表,以编程方式提供信息

iphone - iOS - 返回匹配命名方案的文件数

ios - 算术运算符和键值编码

iphone - 我的 UITable 正在重复使用不应该使用的单元格!

ios - 不从方法触发 UIButton 的事件处理程序

ios - 如何知道文件正在通过 NSFileManager 写入

ios - 如何更改永远运行的 SKAction

iphone:根据主视图中选择的单元格在详细 View 中显示数据

ios - 如何从 tableView 重新加载表格?