Get Threading Right with DiffUtil
Jon F Hancock
3447

The order in which dispatchUpdates works :

protected void dispatchUpdates(List<Item> newItems,
DiffUtil.DiffResult diffResult) {
diffResult.dispatchUpdatesTo(this);
items.clear();
items.addAll(newItems);
}

seems to be opposite to what is recommended in the documenation where the change the list then call dispatchUpdatesTo.

For example, if you have an Adapter that is backed by a List, you can swap the list with the new one then call this method to dispatch all updates to the RecyclerView.
List oldList = mAdapter.getData();
DiffResult result = DiffUtil.calculateDiff(new MyCallback(oldList, newList));
mAdapter.setData(newList);
result.dispatchUpdatesTo(mAdapter);

However Using the documentation’s order result in

java.lang.IndexOutOfBoundsException: Inconsistency detected. Invalid view holder adapter positionViewHolder

Do yo have an explanation for that?

One clap, two clap, three clap, forty?

By clapping more or less, you can signal to us which stories really stand out.