6

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 ?

Charly Lafon
  • 562
  • 1
  • 3
  • 18
  • A couple of thoughts. 1. Have you looked in to `ListAdapter` and `DiffUtil.ItemCallback`? 2. Square is working on [`cycler`](https://github.com/square/cycler) which uses `DiffUtil` and coroutines to do exactly what you want, it may be a good source for reference. – Emmanuel Feb 07 '20 at 18:11
  • 1
    Thank you for cycler, I didn't know about it it looks interesting – Charly Lafon Feb 07 '20 at 20:31

1 Answers1

9

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.

Pawel
  • 15,548
  • 3
  • 36
  • 36
  • Clear explanation and clean solution. Thank you – Charly Lafon Feb 07 '20 at 20:30
  • @Pawel Hey can you please look this [issue](https://stackoverflow.com/q/72742819/11560810) – Kotlin Learner Jun 24 '22 at 11:06
  • Hi @Pawel, I get screen flickering because of calling updateConversationsList too many times from my flow any recommendations ? – Cpp crusaders Jan 10 '23 at 15:40
  • There is a risk if job gets cancelled after: dataset = newDataset but before: diff.dispatchUpdatesTo(this@ConversationsAdapter) – Cpp crusaders Jan 12 '23 at 06:14
  • @Cppcrusaders there's no risk because cancellation state is not checked there. If you're using flow you're probably using `collectLatest` and don't really need to keep the track of a job like in this answer, if you experience flickering you can try to `debounce` your flow or wrap `DiffUtil.calculateDiff(...)` call with `runInterruptible`. – Pawel Jan 12 '23 at 10:26
  • Debounce is a great solution.. The following scenario makes me nervous.. 1> we set dataset = dataset_v2 [so new state] 2> before we could apply he diff by diff.dispatchUpdatesTo the coroutine is cancelled 3> new dataset (lets call it is dataset_v3) is now diffed with dataset_v2 and will be applied to UI... but what was expected was it to be diffed against dataset – Cpp crusaders Jan 13 '23 at 15:50
  • 1
    @Cppcrusaders as I said in previous comment, it won't happen because coroutine cancellation is cooperative and nothing checks cancellation state in between those 2 calls . – Pawel Jan 13 '23 at 19:25