协程、异步 DiffUtil 和不一致检测到的错误

Coroutines, async DiffUtil and Inconsistency detected error

我在组合 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的Flows调用,就像Flow<Conversation>一样。

尽管如此,我有时还是会遇到 java.lang.IndexOutOfBoundsException: Inconsistency detected. Invalid view holder adapter positionViewHolder 错误。阅读 this thread I understand that it is a threading problem and I've read lots of recommendation like this one 都说:更新 Adapter 数据集的线程和将更新分派到 RecyclerView 的线程必须相同。

如您所见,我已经尊重这一点:

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

既然是主线程,而且只有主线程执行这两个操作,我怎么可能会得到这个错误?

你的 diff 正在比赛。如果您的更新在短时间内出现两次,则可能会发生这种情况:

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