# InterProcessCoordinator

# 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)

*** ** * ** ***

Kotlin\|[Java](https://developer.android.com/reference/androidx/datastore/core/InterProcessCoordinator "View this page in Java")
Common/AllAndroid/JVMNative/C/iOSJavaScript
  - Cmn  

  ```
  interface InterProcessCoordinator
  ```

*** ** * ** ***

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[createSingleProcessCoordinator](https://developer.android.com/reference/kotlin/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 functions                                  |
|---------------------------------------------------------------------------------------|------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|-----|
| `suspend `[Int](https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-int/index.html) | [getVersion](https://developer.android.com/reference/kotlin/androidx/datastore/core/InterProcessCoordinator#getVersion())`()` Atomically get the current version.                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                        | Cmn |
| `suspend `[Int](https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-int/index.html) | [incrementAndGetVersion](https://developer.android.com/reference/kotlin/androidx/datastore/core/InterProcessCoordinator#incrementAndGetVersion())`()` Atomically increment version and return the new version.                                                                                                                                                                                                                                                                                                                                                                                                                                                                           | Cmn |
| `suspend T`                                                                           | `<T : `[Any](https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-any/index.html)`?> `[lock](https://developer.android.com/reference/kotlin/androidx/datastore/core/InterProcessCoordinator#lock(kotlin.coroutines.SuspendFunction0))`(block: suspend () `->` T)` Get the exclusive lock shared by the coordinators from DataStore instances (even from different processes) to run a suspending code[block](https://developer.android.com/reference/kotlin/androidx/datastore/core/InterProcessCoordinator#lock(kotlin.coroutines.SuspendFunction0))that returns type`T`.                                                                                                              | Cmn |
| `suspend T`                                                                           | `<T : `[Any](https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-any/index.html)`?> `[tryLock](https://developer.android.com/reference/kotlin/androidx/datastore/core/InterProcessCoordinator#tryLock(kotlin.coroutines.SuspendFunction1))`(block: suspend (`[Boolean](https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-boolean/index.html)`) `->` T)` Attempt to get the exclusive lock shared by the coordinators from DataStore instances (even from different processes) and run the code[block](https://developer.android.com/reference/kotlin/androidx/datastore/core/InterProcessCoordinator#tryLock(kotlin.coroutines.SuspendFunction1))regardless of the attempt result. | Cmn |

|                                                                                           ### Public properties                                                                                           |
|-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|-----|
| [Flow](https://kotlinlang.org/api/kotlinx.coroutines/kotlinx-coroutines-core/kotlinx.coroutines.flow/-flow/index.html)`<`[Unit](https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-unit/index.html)`>` | [updateNotifications](https://developer.android.com/reference/kotlin/androidx/datastore/core/InterProcessCoordinator#updateNotifications()) A flow that emits a Unit when the data for the DataStore changes. | Cmn |

## Public functions

### getVersion

Cmn  

```
suspend fun getVersion(): Int
```

Atomically get the current version.[DataStore](https://developer.android.com/reference/kotlin/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

Cmn  

```
suspend fun incrementAndGetVersion(): Int
```

Atomically increment version and return the new version.[DataStore](https://developer.android.com/reference/kotlin/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

Cmn  

```
suspend fun <T : Any?> lock(block: suspend () -> T): T
```

Get the exclusive lock shared by the coordinators from DataStore instances (even from different processes) to run a suspending code[block](https://developer.android.com/reference/kotlin/androidx/datastore/core/InterProcessCoordinator#lock(kotlin.coroutines.SuspendFunction0))that returns type`T`. It guarantees one-at-a-time execution for all the[block](https://developer.android.com/reference/kotlin/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         |
|----------------------------|-------------------------------------------------------------|
| `block: suspend () `->` T` | The block of code that is performed with the lock resource. |

### tryLock

Cmn  

```
suspend fun <T : Any?> tryLock(block: suspend (Boolean) -> T): T
```

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

|                                                   Parameters                                                    |
|-----------------------------------------------------------------------------------------------------------------|----------------------------------------------------------------------------------------------------------------------------------------------------------------|
| `block: suspend (`[Boolean](https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-boolean/index.html)`) `->` T` | 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. |

## Public properties

### updateNotifications

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

```
val updateNotifications: Flow<Unit>
```

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