我在组合Kotlin Flows和异步DiffUtil时遇到麻烦。

我在RecyclerView.Adapter中具有此功能,该功能在计算线程上计算DiffUtil并将更新分发到Main线程上的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的Flows调用,例如Flow<Conversation>

现在,尽管如此,有时我还是遇到了java.lang.IndexOutOfBoundsException: Inconsistency detected. Invalid view holder adapter positionViewHolder错误。阅读this thread我了解这是一个线程问题,并且我阅读了很多类似this one的建议,这些建议都说:更新适配器数据集的线程和将更新分发给RecyclerView的线程必须相同。

如您所见,我已经做到了:
withContext(Dispatchers.Main) {
    dataset = newDataset
    diff.dispatchUpdatesTo(this@ConversationsAdapter)
}

由于主线程(只有它)执行这两个操作,所以我怎么可能出现此错误?

最佳答案

你的差异在比赛。如果您的更新在短期内出现两次,则可能会发生:

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)将在内部检查延续状态,并且不会运行。

10-07 23:58