PagingDataAdapter.submitData 未添加数据

PagingDataAdapter.submitData not adding data

我正在使用 Jetpack 库中的 Paging 3,在将数据插入房间数据库后,调用了 collectLatest,但在使用 PagingDataAdapter.submitData 添加数据后,它似乎没有添加到适配器中。我尝试使用 adapter.refresh 和 adpter.notifyDatSetChanged,它也不起作用,下面是完整代码。

viewModel.incomingMessagingEvent.observe(
            viewLifecycleOwner,
            Observer { incomingMessage ->
                incomingMessage?.let {
                    lifecycleScope.launch(Dispatchers.IO) {
                        viewModel.insertNewMessage(incomingMessage)
                    }
                }
            })
suspend fun insertNewMessage(directMessage: DirectMessage) {
        directMessagingRepository.messagesLatestDataBase.directMessageDao().insert(directMessage)
    }
@Entity(tableName = "direct_message_table")
@TypeConverters(LumTypeConverters::class)
@Parcelize
data class DirectMessage(

    @PrimaryKey(autoGenerate = true)
    val dbRowId: Int = 0,

    @SerializedName("id") val id: Int? = null,
    @SerializedName("text") val text: String? = null,
    @SerializedName("group_id") val group_id: Int? = null,
    @SerializedName("user_id") val user_id: Int? = null,
    @SerializedName("message_type") val message_type: DirectMessageType? = null,
    @SerializedName("message_images") val message_images: List<LumAsset>? = null,
    @SerializedName("playlist") val playList: Playlist? = null,
    @SerializedName("user") val user: User? = null,
    @SerializedName("album") val album: Album? = null,
    @SerializedName("media") val media: Track? = null,
    @SerializedName("message_has_media") val message_has_media: Boolean? = null,
    @SerializedName("source_media_id") val source_media_id: Int? = null,
    @SerializedName("source_activity_id") val source_activity_id: Int? = null,
    @SerializedName("source_user_id") val source_user_id: Int? = null,
    @SerializedName("source_album_id") val source_album_id: Int? = null,
    @SerializedName("author_name") val author_name: String? = null,
    @SerializedName("author_handle") val author_handle: String? = null,
    @SerializedName("author_role_id") val author_role_id: Int? = null,
    @SerializedName("author_user_id") val author_user_id: Int? = null,
    @SerializedName("author_profile_url") val author_profile_url: String? = null,
    @SerializedName("created_at") val created_at: String? = null,
    @SerializedName("message_attachment_subtitle") val message_attachment_subtitle: String? = null,
    @SerializedName("author") val author: Author? = null,
    @SerializedName("activity") val activity: FeedActivity? = null
) : Parcelable
@Dao
interface DirectMessageDao {

    @Insert(onConflict = OnConflictStrategy.REPLACE)
    suspend fun insert(directMessage: DirectMessage)

    @Query("SELECT * FROM direct_message_table")
    suspend fun getDirectMessages(): List<DirectMessage>

    @Query("SELECT * FROM direct_message_table where id=:messageId ")
    suspend fun getGroupOnId(messageId: Int): List<DirectMessage>

    @Query("SELECT * FROM direct_message_table")
    fun getDirectMessageAsPagingSource(): PagingSource<Int, DirectMessage>

    @Query("SELECT * FROM direct_message_table")
    fun getDirectMessageList(): DataSource.Factory<Int, DirectMessage>

    @Insert
    suspend fun insertAll(groups: List<DirectMessage>)

    @Delete
    suspend fun delete(directMessageGroup: DirectMessage)

    @Query("DELETE FROM direct_message_table")
    suspend fun deleteAll()

    @Query("DELETE FROM direct_message_table where id=:messageId")
    suspend fun deleteGroup(messageId: Int)

    @Update
    suspend fun update(directMessageGroup: DirectMessage)
}
lateinit var conversationsFlow: Flow<PagingData<DirectMessage>>

    fun getConversationsAsFlow(groupId: Int) {
        conversationsFlow = Pager(
            PagingConfig(
                prefetchDistance = LumConstants.PAGE_SIZE,
                initialLoadSize = LumConstants.INITIAL_LOAD_ITEMS,
                pageSize = LumConstants.PAGE_SIZE
            ),
            remoteMediator = GroupConversationRemoteMediator(
                groupId,
                directMessagingRepository.messagesLatestDataBase,
                NetworkService.directMessageNetworkInterface
            )
        ) {
            directMessagingRepository.messagesLatestDataBase.directMessageDao()
                .getDirectMessageAsPagingSource()
        }.flow
    }
class GroupConversationRemoteMediator(
    private val groupId: Int,
    private val database: MessagesDatabase,
    private val directMessageNetworkInterface: DirectMessageNetworkInterface
) : RemoteMediator<Int, DirectMessage>() {

    private val dao = database.directMessageDao()
    var nextUrl: String? = null

    override suspend fun load(
        loadType: LoadType,
        state: PagingState<Int, DirectMessage>
    ): MediatorResult {

        try {
            // Get the closest item from PagingState that we want to load data around.
            val loadKey = when (loadType) {
                LoadType.REFRESH -> null
                LoadType.PREPEND -> return MediatorResult.Success(endOfPaginationReached = true)
                LoadType.APPEND -> {
                    if (nextUrl == null) {
                        return MediatorResult.Success(endOfPaginationReached = true)
                    }
                    nextUrl
                }
            }

            val data =
                if (loadKey != null)
                    directMessageNetworkInterface.getMoreConversationsAsync(loadKey)
                        .await()
                else
                    directMessageNetworkInterface.getConversationsAsync(groupId).await()

            database.withTransaction {
                if (loadType == LoadType.REFRESH) {
                    dao.deleteAll()
                    nextUrl = null
                }

                if (data.success) {
                    nextUrl = data.data.list.next_page_url
                    dao.insertAll(data.data.list.data.map {
                        it
                    })
                } else {
                    nextUrl = null
                }

            }

            return MediatorResult.Success(endOfPaginationReached = if (data.success) data.data.list.data.isEmpty() else true)
        } catch (e: IOException) {
            return MediatorResult.Error(e)
        } catch (e: HttpException) {
            return MediatorResult.Error(e)
        }
    }
}
        viewModel.conversationsFlow.collectLatest {
               adapter.submitData(it)
           
        }
class ChatDiffUtilCallback : DiffUtil.ItemCallback<DirectMessage>() {
    override fun areItemsTheSame(
        oldItem: DirectMessage,
        newItem: DirectMessage
    ): Boolean =
        oldItem.id == newItem.id

    override fun areContentsTheSame(
        oldItem: DirectMessage,
        newItem: DirectMessage
    ): Boolean =
        oldItem == newItem
}

我觉得很蠢,数据已经在更新了,但是我看不到它,因为 recyclerview 的“reverseLayout”设置为 true,而且数据没有按 Dao 中的 id 排序,之后清除那些,问题就解决了。