Depending on the number of items into the list, DiffUtil.calculateDiff
can take a significant amount of time to execute (more than the 16ms allowed).
Since you don't want to block the UI Thread, you'd better move this function call to another thread, then dispatch the result on the UI Thread.
Of course, you could use any technique you'd like : AsyncTask
, RxJava Schedulers, or moving to another coroutine context with withContext(Dispatchers.Default)
.
But this pattern is sooooo common that it has been implemented in the RecyclerView library : ListAdapter.
When updating items of ListAdapter
with submitList
, the difference is calculated on a background thread, you don't need to call calculateDiff
and dispatchUpdatesTo
anymore.