Android 分页库:如果相关项目不在本地数据库中并且不中断分页,如何显示相关项目?

Android Paging Library: how to show related item if it's not in local database yet and don't break paging?

我正在编写聊天应用程序。使用 Android 分页库按页面(项目键控 - message_id)加载消息。还有 "reply" 功能,这意味着用户可以回复聊天中的任何消息。在 RecyclerView 中它应该是这样的:

Part of message which was replied...
------------------------------------
Main message text

很简单,当相关消息已经在本地数据库中时。我可以从数据库中获取所有数据并将其显示在应用程序中。但是可能会出现回复消息不在本地数据库中的情况(例如,这是旧消息,我们刚刚安装了应用程序 - 数据库为空)。消息看起来很糟糕,直到回复的消息不会存储在本地数据库中:


------------------------------------
Main message text

我试过的方法: 如果消息的 属性 reply_to 不为空,我通过此 ID 从远程服务器加载回复消息并将其存储在本地数据库。房间对变化做出反应并显示所有内容。但是这条回复的消息成为我的消息数组中的最后一条消息,并且 BoundaryCallback 在该回复消息之后加载消息,在 REAL 最后一条消息和该回复消息之间跳过消息。

例如,我有 ID 为 1..100 的消息。

(boundary callback makes initialLoad, say 20 items)
1 message
2 message
..
10 message with reply to 50 message (load message 50 and store it in db)
11 message
..
(early loaded messages)
..
20 message
(this should be end, but this is not because of 50 message which is stored in local database now)
50 message
(bounary callback makes loadAfter new items by key - message id)
51 message
..

因此,我们丢失了从 21 到 49 的部分消息。

留言室实体具有以下属性:

@Entity
class Message {
    var id: Int
    var text: String
    var reply_to: Int?
}

MessageModel 由 Room 按关系返回并在 PagedAdapter 中使用

class MessageModel {
    var message: Message
    var repliedMessage: Message
}

如何显示已回复的消息而不中断分页? 应该如何以正确的方式完成?

我找到了解决办法。在我的例子中,我创建了额外的 table RepliedMessage(这是 Message 的绝对副本)并且我将回复的消息存储在 table 中。分页效果很好。我们只需要使 RepliedMessages 保持最新。