# InterProcessCoordinator

Artifact: [androidx.datastore:datastore-core](https://developer.android.com/jetpack/androidx/releases/datastore) [View Source](https://cs.android.com/search?q=file:androidx/datastore/core/InterProcessCoordinator.kt+class:androidx.datastore.core.InterProcessCoordinator) Added in [1.1.0](https://developer.android.com/jetpack/androidx/releases/datastore#1.1.0)

*** ** * ** ***


```
public interface InterProcessCoordinator
```

<br />

*** ** * ** ***

InterProcessCoordinator provides functionalities that support DataStore instances to coordinate the concurrent work running on multiple threads and multiple processes to guarantee its data consistency. Typically users should use default coordinators provided by the library, including `https://developer.android.com/reference/androidx/datastore/core/package-summary#createSingleProcessCoordinator(kotlin.String)` for use cases where DataStore is only used in a single process, and createMultiProcessCoordinator for a DataStore that needs to be accessed in multiple processes.

## Summary

| ### Public methods |
|---|---|
| `abstract @https://developer.android.com/reference/androidx/annotation/NonNull https://kotlinlang.org/api/kotlinx.coroutines/kotlinx-coroutines-core/kotlinx.coroutines.flow/-flow/index.html<https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-unit/index.html>` | `https://developer.android.com/reference/androidx/datastore/core/InterProcessCoordinator#getUpdateNotifications()()` A flow that emits a Unit when the data for the DataStore changes. |
| `abstract int` | `https://developer.android.com/reference/androidx/datastore/core/InterProcessCoordinator#getVersion()()` Atomically get the current version. |
| `abstract int` | `https://developer.android.com/reference/androidx/datastore/core/InterProcessCoordinator#incrementAndGetVersion()()` Atomically increment version and return the new version. |
| `abstract @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/datastore/core/InterProcessCoordinator#lock(kotlin.coroutines.SuspendFunction0)(@https://developer.android.com/reference/androidx/annotation/NonNull SuspendFunction0<@https://developer.android.com/reference/androidx/annotation/NonNull T> block)` Get the exclusive lock shared by the coordinators from DataStore instances (even from different processes) to run a suspending code `https://developer.android.com/reference/androidx/datastore/core/InterProcessCoordinator#lock(kotlin.coroutines.SuspendFunction0)` that returns type `T`. |
| `abstract @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/datastore/core/InterProcessCoordinator#tryLock(kotlin.coroutines.SuspendFunction1)( @https://developer.android.com/reference/androidx/annotation/NonNull SuspendFunction1<@https://developer.android.com/reference/androidx/annotation/NonNull https://developer.android.com/reference/java/lang/Boolean.html, @https://developer.android.com/reference/androidx/annotation/NonNull T> block )` Attempt to get the exclusive lock shared by the coordinators from DataStore instances (even from different processes) and run the code `https://developer.android.com/reference/androidx/datastore/core/InterProcessCoordinator#tryLock(kotlin.coroutines.SuspendFunction1)` regardless of the attempt result. |

## Public methods

### getUpdateNotifications

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

```
abstract @NonNull Flow<Unit> getUpdateNotifications()
```

A flow that emits a Unit when the data for the DataStore changes. `https://developer.android.com/reference/androidx/datastore/core/DataStore` collects this flow to signal the action to invalidate cache and re-read data from disk.

### getVersion

```
abstract int getVersion()
```

Atomically get the current version. `https://developer.android.com/reference/androidx/datastore/core/DataStore` instances for the same data use this method to access the shared version for its cached data and internal state. Notice concurrent access to the version should guarantee data consistency.

### incrementAndGetVersion

```
abstract int incrementAndGetVersion()
```

Atomically increment version and return the new version. `https://developer.android.com/reference/androidx/datastore/core/DataStore` instances for the same data use this method to access the shared version for its cached data and internal state. Notice concurrent access to the version should guarantee data consistency.

Note that the number of calls to the `incrementAndGetVersion` is an internal implementation detail for DataStore and implementers of this API should not make any assumption based on the number of version increments.

### lock

```
abstract @NonNull T <T extends Object> lock(@NonNull SuspendFunction0<@NonNull T> block)
```

Get the exclusive lock shared by the coordinators from DataStore instances (even from different processes) to run a suspending code `https://developer.android.com/reference/androidx/datastore/core/InterProcessCoordinator#lock(kotlin.coroutines.SuspendFunction0)` that returns type `T`. It guarantees one-at-a-time execution for all the `https://developer.android.com/reference/androidx/datastore/core/InterProcessCoordinator#lock(kotlin.coroutines.SuspendFunction0)` called with this method. If some other process or thread is holding the lock, it will wait until the lock is available.

| Parameters |
|---|---|
| `@https://developer.android.com/reference/androidx/annotation/NonNull SuspendFunction0<@https://developer.android.com/reference/androidx/annotation/NonNull T> block` | The block of code that is performed with the lock resource. |

### tryLock

```
abstract @NonNull T <T extends Object> tryLock(
    @NonNull SuspendFunction1<@NonNull Boolean, @NonNull T> block
)
```

Attempt to get the exclusive lock shared by the coordinators from DataStore instances (even from different processes) and run the code `https://developer.android.com/reference/androidx/datastore/core/InterProcessCoordinator#tryLock(kotlin.coroutines.SuspendFunction1)` regardless of the attempt result. Pass a boolean to `https://developer.android.com/reference/androidx/datastore/core/InterProcessCoordinator#tryLock(kotlin.coroutines.SuspendFunction1)` to indicate if the attempt succeeds. If the attempt fails, `https://developer.android.com/reference/androidx/datastore/core/InterProcessCoordinator#tryLock(kotlin.coroutines.SuspendFunction1)` will run immediately after the attempt, without waiting for the lock to become available.

| Parameters |
|---|---|
| `@https://developer.android.com/reference/androidx/annotation/NonNull SuspendFunction1<@https://developer.android.com/reference/androidx/annotation/NonNull https://developer.android.com/reference/java/lang/Boolean.html, @https://developer.android.com/reference/androidx/annotation/NonNull T> block` | The block of code that is performed after attempting to get the lock resource. Block will receive a Boolean parameter which is true if the try lock succeeded. |