RLMArray上的领域通知不允许我更新表

时间:2016-06-22 23:18:34

标签: objective-c realm

我正在尝试在发送新的聊天消息时更新我的​​表格视图。我在我的集​​合上设置了一个通知块来通知表视图开始更新但是我一直收到一个错误,说有0个插入:

Terminating app due to uncaught exception 'NSInternalInconsistencyException', 
reason: 'Invalid update: invalid number of rows in section 0.  
The number of rows contained in an existing section after the update (8)
must be equal to the number of rows contained in that section before the update (7), 
plus or minus the number of rows inserted or deleted from that section (0 inserted, 0 deleted)
and plus or minus the number of rows moved into or out of that section (0 moved in, 0 moved out).'

这是我的通知代码:

- (void)setUpRealmNotifications {
// Observe RLMResults Notifications
__weak typeof(self) weakSelf = self;
self.notificationToken = [[TERMessage objectsWhere:@"conversationID == %@", self.model.serverID] addNotificationBlock:^(RLMResults<TERMessage *> *results, RLMCollectionChange *change, NSError *error) {
    if (error) {
        NSLog(@"Failed to open Realm on background worker: %@", error);
        return;
    }

    UITableView *tableView = weakSelf.tableView;
    // Initial run of the query will pass nil for the change information
    if (!change) {
        [tableView reloadData];
        return;
    }

    // Query results have changed, so apply them to the UITableView
    [tableView beginUpdates];
    [tableView deleteRowsAtIndexPaths:[change deletionsInSection:0]
                     withRowAnimation:UITableViewRowAnimationAutomatic];
    [tableView insertRowsAtIndexPaths:[change insertionsInSection:0]
                     withRowAnimation:UITableViewRowAnimationAutomatic];
    [tableView reloadRowsAtIndexPaths:[change modificationsInSection:0]
                     withRowAnimation:UITableViewRowAnimationAutomatic];
    [tableView endUpdates];
}];
}

1 个答案:

答案 0 :(得分:1)

从它的外观来看,更改通知正在降低并正确更新UITableView,但您的UITableView数据源似乎与相同的更改不匹配。

为了确保更改通知的结果和负责管理单元格的UITableView数据源方法不会失去同步,我建议保留一个{{1变量通知和表数据源都引用的实例。

RLMResults