Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Coroutines, async DiffUtil and Inconsistency detected error

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 ?

like image 959
Charly Lafon Avatar asked Feb 07 '20 17:02

Charly Lafon


People also ask

What is the difference between coroutines and async methods?

Both fulfil different needs. Coroutines are useful for executing methods over a number of frames. Async methods are useful for executing methods after a given task has finished. e.g. async methods can commonly used to wait for I/O operations to complete. Coroutines can be used to move an object each frame.

What is an asyncdiffutil in Python?

The general concept revolves around the use of a Coroutine and the Actor pattern. Each new list update is dispatched to the AsyncDiffUtil. This can be a null list, and empty list, or one that contains several items. The AsyncDiffUtil transforms this into a UpdateOperation — which can either be an Insert or Clear.

Will unity replace coroutines with async/await?

Unity devs have said they may replace Coroutines with an Async/Await style alternative, but they do not currently have any plans to, especially until they is a distinct reason to. alamaxsong, PLDeveloper and Reeley like this. That makes sense. Lets say i want to Destroy a gameobject after 5 seconds.

What are the warnings on Compile for Async methods?

The above example produces these warnings on compile: `warning CS4014: Because this call is not awaited, execution of the current method continues before the call is completed. Consider applying the 'await' operator to the result of the call.` `warning CS1998: This async method lacks 'await' operators and will run synchronously.


Video Answer


1 Answers

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.

like image 139
Pawel Avatar answered Sep 30 '22 12:09

Pawel