# DiffUtil.DiffResult

# DiffUtil.DiffResult

Artifact:[androidx.recyclerview:recyclerview](https://developer.android.com/jetpack/androidx/releases/recyclerview)  
[View Source](https://cs.android.com/search?q=file:androidx/recyclerview/widget/DiffUtil.java+class:androidx.recyclerview.widget.DiffUtil.DiffResult)  
Added in[1.0.0](https://developer.android.com/jetpack/androidx/releases/recyclerview#1.0.0)

*** ** * ** ***

Kotlin\|[Java](https://developer.android.com/reference/androidx/recyclerview/widget/DiffUtil.DiffResult "View this page in Java")

<br />

```
class DiffUtil.DiffResult
```

<br />

*** ** * ** ***

This class holds the information about the result of a[calculateDiff](https://developer.android.com/reference/kotlin/androidx/recyclerview/widget/DiffUtil#calculateDiff(androidx.recyclerview.widget.DiffUtil.Callback,boolean))call.

You can consume the updates in a DiffResult via[dispatchUpdatesTo](https://developer.android.com/reference/kotlin/androidx/recyclerview/widget/DiffUtil.DiffResult#dispatchUpdatesTo(androidx.recyclerview.widget.ListUpdateCallback))or directly stream the results into a[RecyclerView.Adapter](https://developer.android.com/reference/kotlin/androidx/recyclerview/widget/RecyclerView.Adapter)via[dispatchUpdatesTo](https://developer.android.com/reference/kotlin/androidx/recyclerview/widget/DiffUtil.DiffResult#dispatchUpdatesTo(androidx.recyclerview.widget.RecyclerView.Adapter)).

## Summary

|                                    ### Constants                                    |
|-------------------------------------------------------------------------------------|--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|
| `const `[Int](https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-int/index.html) | [NO_POSITION](https://developer.android.com/reference/kotlin/androidx/recyclerview/widget/DiffUtil.DiffResult#NO_POSITION())` = -1` Signifies an item not present in the list. |

|                             ### Public functions                              |
|-------------------------------------------------------------------------------|--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|
| [Int](https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-int/index.html)   | [convertNewPositionToOld](https://developer.android.com/reference/kotlin/androidx/recyclerview/widget/DiffUtil.DiffResult#convertNewPositionToOld(int))`(newListPosition: @`[IntRange](https://developer.android.com/reference/kotlin/androidx/annotation/IntRange)`(from = 0) `[Int](https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-int/index.html)`)` Given a position in the new list, returns the position in the old list, or`NO_POSITION`if it was removed. |
| [Int](https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-int/index.html)   | [convertOldPositionToNew](https://developer.android.com/reference/kotlin/androidx/recyclerview/widget/DiffUtil.DiffResult#convertOldPositionToNew(int))`(oldListPosition: @`[IntRange](https://developer.android.com/reference/kotlin/androidx/annotation/IntRange)`(from = 0) `[Int](https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-int/index.html)`)` Given a position in the old list, returns the position in the new list, or`NO_POSITION`if it was removed. |
| [Unit](https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-unit/index.html) | [dispatchUpdatesTo](https://developer.android.com/reference/kotlin/androidx/recyclerview/widget/DiffUtil.DiffResult#dispatchUpdatesTo(androidx.recyclerview.widget.RecyclerView.Adapter))`(adapter: `[RecyclerView.Adapter](https://developer.android.com/reference/kotlin/androidx/recyclerview/widget/RecyclerView.Adapter)`)` Dispatches the update events to the given adapter.                                                                                      |
| [Unit](https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-unit/index.html) | [dispatchUpdatesTo](https://developer.android.com/reference/kotlin/androidx/recyclerview/widget/DiffUtil.DiffResult#dispatchUpdatesTo(androidx.recyclerview.widget.ListUpdateCallback))`(updateCallback: `[ListUpdateCallback](https://developer.android.com/reference/kotlin/androidx/recyclerview/widget/ListUpdateCallback)`)` Dispatches update operations to the given Callback.                                                                                    |

## Constants

### NO_POSITION

Added in[1.0.0](https://developer.android.com/jetpack/androidx/releases/recyclerview#1.0.0)  

```
const val NO_POSITION = -1: Int
```

Signifies an item not present in the list.  

## Public functions

### convertNewPositionToOld

Added in[1.0.0](https://developer.android.com/jetpack/androidx/releases/recyclerview#1.0.0)  

```
fun convertNewPositionToOld(newListPosition: @IntRange(from = 0) Int): Int
```

Given a position in the new list, returns the position in the old list, or`NO_POSITION`if it was removed.  

|                                                                                             Parameters                                                                                              |
|-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|------------------------------|
| `newListPosition: @`[IntRange](https://developer.android.com/reference/kotlin/androidx/annotation/IntRange)`(from = 0) `[Int](https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-int/index.html) | Position of item in new list |

|                                   Returns                                   |
|-----------------------------------------------------------------------------|--------------------------------------------------------------|
| [Int](https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-int/index.html) | Position of item in old list, or`NO_POSITION`if not present. |

|                                                                        See also                                                                         |
|---------------------------------------------------------------------------------------------------------------------------------------------------------|---|
| [NO_POSITION](https://developer.android.com/reference/kotlin/androidx/recyclerview/widget/DiffUtil.DiffResult#NO_POSITION())                            |   |
| [convertOldPositionToNew](https://developer.android.com/reference/kotlin/androidx/recyclerview/widget/DiffUtil.DiffResult#convertOldPositionToNew(int)) |   |

### convertOldPositionToNew

Added in[1.0.0](https://developer.android.com/jetpack/androidx/releases/recyclerview#1.0.0)  

```
fun convertOldPositionToNew(oldListPosition: @IntRange(from = 0) Int): Int
```

Given a position in the old list, returns the position in the new list, or`NO_POSITION`if it was removed.  

|                                                                                             Parameters                                                                                              |
|-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|------------------------------|
| `oldListPosition: @`[IntRange](https://developer.android.com/reference/kotlin/androidx/annotation/IntRange)`(from = 0) `[Int](https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-int/index.html) | Position of item in old list |

|                                   Returns                                   |
|-----------------------------------------------------------------------------|--------------------------------------------------------------|
| [Int](https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-int/index.html) | Position of item in new list, or`NO_POSITION`if not present. |

|                                                                        See also                                                                         |
|---------------------------------------------------------------------------------------------------------------------------------------------------------|---|
| [NO_POSITION](https://developer.android.com/reference/kotlin/androidx/recyclerview/widget/DiffUtil.DiffResult#NO_POSITION())                            |   |
| [convertNewPositionToOld](https://developer.android.com/reference/kotlin/androidx/recyclerview/widget/DiffUtil.DiffResult#convertNewPositionToOld(int)) |   |

### dispatchUpdatesTo

Added in[1.0.0](https://developer.android.com/jetpack/androidx/releases/recyclerview#1.0.0)  

```
fun dispatchUpdatesTo(adapter: RecyclerView.Adapter): Unit
```

Dispatches the update events to the given adapter.

For example, if you have an[Adapter](https://developer.android.com/reference/kotlin/androidx/recyclerview/widget/RecyclerView.Adapter)that is backed by a[List](https://developer.android.com/reference/java/util/List.html), you can swap the list with the new one then call this method to dispatch all updates to the RecyclerView.  

```kotlin
    List oldList = mAdapter.getData();
    DiffResult result = DiffUtil.calculateDiff(new MyCallback(oldList, newList));
    mAdapter.setData(newList);
    result.dispatchUpdatesTo(mAdapter);
```

Note that the RecyclerView requires you to dispatch adapter updates immediately when you change the data (you cannot defer`notify*`calls). The usage above adheres to this rule because updates are sent to the adapter right after the backing data is changed, before RecyclerView tries to read it.

On the other hand, if you have another[AdapterDataObserver](https://developer.android.com/reference/kotlin/androidx/recyclerview/widget/RecyclerView.AdapterDataObserver)that tries to process events synchronously, this may confuse that observer because the list is instantly moved to its final state while the adapter updates are dispatched later on, one by one. If you have such an[AdapterDataObserver](https://developer.android.com/reference/kotlin/androidx/recyclerview/widget/RecyclerView.AdapterDataObserver), you can use[dispatchUpdatesTo](https://developer.android.com/reference/kotlin/androidx/recyclerview/widget/DiffUtil.DiffResult#dispatchUpdatesTo(androidx.recyclerview.widget.ListUpdateCallback))to handle each modification manually.  

|                                                             Parameters                                                              |
|-------------------------------------------------------------------------------------------------------------------------------------|--------------------------------------------------------------------------------------------------|
| `adapter: `[RecyclerView.Adapter](https://developer.android.com/reference/kotlin/androidx/recyclerview/widget/RecyclerView.Adapter) | A RecyclerView adapter which was displaying the old list and will start displaying the new list. |

|                                                              See also                                                              |
|------------------------------------------------------------------------------------------------------------------------------------|---|
| [AdapterListUpdateCallback](https://developer.android.com/reference/kotlin/androidx/recyclerview/widget/AdapterListUpdateCallback) |   |

### dispatchUpdatesTo

Added in[1.0.0](https://developer.android.com/jetpack/androidx/releases/recyclerview#1.0.0)  

```
fun dispatchUpdatesTo(updateCallback: ListUpdateCallback): Unit
```

Dispatches update operations to the given Callback.

These updates are atomic such that the first update call affects every update call that comes after it (the same as RecyclerView).  

|                                                               Parameters                                                               |
|----------------------------------------------------------------------------------------------------------------------------------------|------------------------------------------------|
| `updateCallback: `[ListUpdateCallback](https://developer.android.com/reference/kotlin/androidx/recyclerview/widget/ListUpdateCallback) | The callback to receive the update operations. |

|                                                                                         See also                                                                                          |
|-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|---|
| [dispatchUpdatesTo](https://developer.android.com/reference/kotlin/androidx/recyclerview/widget/DiffUtil.DiffResult#dispatchUpdatesTo(androidx.recyclerview.widget.RecyclerView.Adapter)) |   |