5

我无法将 Kotlin Flows 和异步 DiffUtil 放在一起。

我的 RecyclerView.Adapter 中有这个函数,它在计算线程上计算 DiffUtil 并将更新发送到主线程上的 RecyclerView:

suspend fun updateDataset(newDataset: List<Item>) = withContext(Dispatchers.Default) {
        val diff = DiffUtil.calculateDiff(object : DiffUtil.Callback()
        {
            override fun areItemsTheSame(oldItemPosition: Int, newItemPosition: Int): Boolean
                    = dataset[oldItemPosition].conversation.id == newDataset[newItemPosition].conversation.id

            override fun getOldListSize(): Int = dataset.size
            override fun getNewListSize(): Int = newDataset.size

            override fun areContentsTheSame(oldItemPosition: Int, newItemPosition: Int): Boolean
                    = dataset[oldItemPosition] == newDataset[newItemPosition]
        })

        withContext(Dispatchers.Main) {
            dataset = newDataset // <-- dataset is the Adapter's dataset
            diff.dispatchUpdatesTo(this@ConversationsAdapter)
        }
    }

我像这样从我的 Fragment 调用这个函数:

private fun updateConversationsList(conversations: List<ConversationsAdapter.Item>)
{
    viewLifecycleOwner.lifecycleScope.launch {
        (listConversations.adapter as ConversationsAdapter).updateDataset(conversations)
    }
}

updateConversationsList()在很短的时间内被多次调用,因为这个函数是由 Kotlin 的Flowslike调用的Flow<Conversation>

现在有了所有这些,我有时会遇到java.lang.IndexOutOfBoundsException: Inconsistency detected. Invalid view holder adapter positionViewHolder错误。阅读这个线程我知道这是一个线程问题,我已经阅读了很多这样的推荐更新适配器数据集的线程和向 RecyclerView 分派更新的线程必须相同。

如您所见,我已经通过以下方式尊重了这一点:

withContext(Dispatchers.Main) {
    dataset = newDataset
    diff.dispatchUpdatesTo(this@ConversationsAdapter)
}

由于主线程,只有它,这两个操作,我怎么可能得到这个错误?

4

1 回答 1

7

你的差异正在赛车。如果您的更新在短时间内出现两次,这可能会发生:

Adapter has dataset 1 @Main
Dataset 2 comes
calculateDiff between 1 & 2 @Async
Dataset 3 comes
calculateDiff between 1 & 3 @Async
finished calculating diff between 1 & 2 @ Async
finished calculating diff between 1 & 3 @ Async
Dispatcher main starts handling messages
replace dataset 1 with dataset 2 using 1-2 diff @Main
replace dataset 2 with dataset 3 using 1-3 diff @Main - inconsistency

替代方案是 1-3 之间的差异可以在 1-2 之前完成,但问题仍然存在。您必须在新的计算出现时取消正在进行的计算并防止部署无效的差异,例如在您的片段中存储作业引用:

var updateJob : Job? = null

private fun updateConversationsList(conversations: List<ConversationsAdapter.Item>)
{
    updateJob?.cancel()
    updateJob = viewLifecycleOwner.lifecycleScope.launch {
        (listConversations.adapter as ConversationsAdapter).updateDataset(conversations)
    }
}

如果您取消它,那么withContext(Dispatchers.Main)将在内部检查继续状态并且不会运行。

于 2020-02-07T17:53:45.490 回答