# CachedPagingDataKt

Added in 3.0.0

*** ** * ** ***


```
public final class CachedPagingDataKt
```

<br />

*** ** * ** ***

## Summary

| ### Public methods |
|---|---|
| `static final @https://developer.android.com/reference/androidx/annotation/NonNull https://kotlinlang.org/api/kotlinx.coroutines/kotlinx-coroutines-core/kotlinx.coroutines.flow/-flow/index.html<@https://developer.android.com/reference/androidx/annotation/NonNull https://developer.android.com/reference/androidx/paging/PagingData<@https://developer.android.com/reference/androidx/annotation/NonNull T>>` | `<T extends https://developer.android.com/reference/java/lang/Object.html> https://developer.android.com/reference/androidx/paging/CachedPagingDataKt#(kotlinx.coroutines.flow.Flow).cachedIn(kotlinx.coroutines.CoroutineScope)( @https://developer.android.com/reference/androidx/annotation/NonNull https://kotlinlang.org/api/kotlinx.coroutines/kotlinx-coroutines-core/kotlinx.coroutines.flow/-flow/index.html<@https://developer.android.com/reference/androidx/annotation/NonNull https://developer.android.com/reference/androidx/paging/PagingData<@https://developer.android.com/reference/androidx/annotation/NonNull T>> receiver, @https://developer.android.com/reference/androidx/annotation/NonNull https://kotlinlang.org/api/kotlinx.coroutines/kotlinx-coroutines-core/kotlinx.coroutines/-coroutine-scope/index.html scope )` Caches the `https://developer.android.com/reference/androidx/paging/PagingData` such that any downstream collection from this flow will share the same `https://developer.android.com/reference/androidx/paging/PagingData`. |

## Public methods

### cachedIn

Artifact: [androidx.paging:paging-common](https://developer.android.com/jetpack/androidx/releases/paging) [View Source](https://cs.android.com/search?q=file:androidx/paging/CachedPagingData.kt+function:cachedIn) Added in [3.0.0](https://developer.android.com/jetpack/androidx/releases/paging#3.0.0)

```
public static final @NonNull Flow<@NonNull PagingData<@NonNull T>> <T extends Object> cachedIn(
    @NonNull Flow<@NonNull PagingData<@NonNull T>> receiver,
    @NonNull CoroutineScope scope
)
```

Caches the `https://developer.android.com/reference/androidx/paging/PagingData` such that any downstream collection from this flow will share the same `https://developer.android.com/reference/androidx/paging/PagingData`.

The flow is kept active as long as the given `https://developer.android.com/reference/androidx/paging/package-summary#(kotlinx.coroutines.flow.Flow).cachedIn(kotlinx.coroutines.CoroutineScope)` is active. To avoid leaks, make sure to use a `https://developer.android.com/reference/androidx/paging/package-summary#(kotlinx.coroutines.flow.Flow).cachedIn(kotlinx.coroutines.CoroutineScope)` that is already managed (like a ViewModel scope) or manually cancel it when you don't need paging anymore.

A common use case for this caching is to cache `https://developer.android.com/reference/androidx/paging/PagingData` in a ViewModel. This can ensure that, upon configuration change (e.g. rotation), then new Activity will receive the existing data immediately rather than fetching it from scratch.

Calling `https://developer.android.com/reference/androidx/paging/package-summary#(kotlinx.coroutines.flow.Flow).cachedIn(kotlinx.coroutines.CoroutineScope)` is required to allow calling androidx.paging.AsyncPagingDataAdapter on the same instance of `https://developer.android.com/reference/androidx/paging/PagingData` emitted by `https://developer.android.com/reference/androidx/paging/Pager` or any of its transformed derivatives, as reloading data from scratch on the same generation of `https://developer.android.com/reference/androidx/paging/PagingData` is an unsupported operation.

Note that this does not turn the `Flow<PagingData>` into a hot stream. It won't execute any unnecessary code unless it is being collected.

```kotlin
import androidx.activity.viewModels
import androidx.appcompat.app.AppCompatActivity
import androidx.lifecycle.ViewModel
import androidx.lifecycle.lifecycleScope
import androidx.lifecycle.viewModelScope
import androidx.paging.Pager
import androidx.paging.PagingConfig
import androidx.paging.cachedIn
import androidx.paging.map

class MyViewModel : ViewModel() {
    val flow =
        Pager(config = PagingConfig(pageSize = 40), pagingSourceFactory = pagingSourceFactory)
            .flow
            // Loads and transformations before the cachedIn operation will be cached, so that
            // multiple observers get the same data. This is true either for simultaneous
            // observers, or e.g. an Activity re-subscribing after being recreated
            .cachedIn(viewModelScope)
}

class MyActivity : AppCompatActivity() {
    val pagingAdapter = MyPagingAdapter()

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        val viewModel by viewModels<MyViewModel>()

        lifecycleScope.launch {
            viewModel.flow
                // Any transformations after the ViewModel's cachedIn step will not be cached,
                // and will instead by re-run immediately on Activity re-creation.
                .map { pagingData ->
                    // example un-cached transformation
                    pagingData.map { UiModel(it) }
                }
                .collectLatest { pagingAdapter.submitData(it) }
        }
    }
}
```

| Parameters |
|---|---|
| `@https://developer.android.com/reference/androidx/annotation/NonNull https://kotlinlang.org/api/kotlinx.coroutines/kotlinx-coroutines-core/kotlinx.coroutines/-coroutine-scope/index.html scope` | The coroutine scope where this page cache will be kept alive. |