【问题标题】:Realm notification on RLMArray not letting me update tablesRLMArray 上的领域通知不允许我更新表
【发布时间】:2016-06-22 23:18:33
【问题描述】:

我目前正在尝试在发送新聊天消息时更新我的​​表格视图。我在我的集​​合上设置了一个通知块来通知表视图开始更新,但我一直收到错误消息,说有 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];
}];
}

【问题讨论】:

    标签: objective-c realm


    【解决方案1】:

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

    为确保更改通知的结果和负责管理单元格的 UITableView 数据源方法不会不同步,我建议在这两个更改周围保留一个 RLMResults 实例通知和表格数据源参考。

    @property (nonatomic, strong) RLMResults *items;
    @property (nonatomic, strong) RLMNotificationToken *token;
    
    // ---
    
    self.items = [TERMessage objectsWhere:@"conversationID == %@", self.model.serverID];
    self.token = [self.items addNotificationBlock:^...];
    
    // ---
    
    - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section 
    {
        return self.items.count;
    }
    
    - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
    {
        TERMessage *message = self.items[indexPath.row];
        UITableViewCell *cell = ...; //configure cell
        return cell;
    }
    

    【讨论】:

    • 谢谢!救了我一些头痛
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多