I'm having trouble putting up together Kotlin Flows and async DiffUtil.
I have this function in my RecyclerView.Adapter that computes on a computation thread a DiffUtil and dispatch updates to the RecyclerView on the Main thread :
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)
}
}
I call this function from my Fragment like this :
private fun updateConversationsList(conversations: List<ConversationsAdapter.Item>)
{
viewLifecycleOwner.lifecycleScope.launch {
(listConversations.adapter as ConversationsAdapter).updateDataset(conversations)
}
}
updateConversationsList()
is called multiple times within a very short period of time because this function is called by Kotlin's Flows
like Flow<Conversation>
.
Now with all that, I'm sometimes getting a java.lang.IndexOutOfBoundsException: Inconsistency detected. Invalid view holder adapter positionViewHolder
error. Reading this thread I understand that it is a threading problem and I've read lots of recommendation like this one that all say : the thread that updates the dataset of the Adapter and the thread that dispatches updates to the RecyclerView must be the same.
As you can see, I already respect this by doing :
withContext(Dispatchers.Main) {
dataset = newDataset
diff.dispatchUpdatesTo(this@ConversationsAdapter)
}
Since the Main thread, and only it, does these two operations, how is it possible that I get this error ?
Your diff is racing. If your update comes twice in short period this can happen:
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
Alternative scenario is diff between 1-3 can finish before 1-2 but issue remains the same. You have to cancel ongoing calculation when new one comes and prevent deploying invalid diff, for example store job reference inside your fragment:
var updateJob : Job? = null
private fun updateConversationsList(conversations: List<ConversationsAdapter.Item>)
{
updateJob?.cancel()
updateJob = viewLifecycleOwner.lifecycleScope.launch {
(listConversations.adapter as ConversationsAdapter).updateDataset(conversations)
}
}
If you cancel it then withContext(Dispatchers.Main)
will internally check continuation state and won't run.