InterProcessCoordinator


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

Atomically get the current version.

suspend Int

Atomically increment version and return the new version.

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

Get the exclusive lock shared by the coordinators from DataStore instances (even from different processes) to run a suspending code block that returns type T.

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

Attempt to get the exclusive lock shared by the coordinators from DataStore instances (even from different processes) and run the code block regardless of the attempt result.

Public properties

Flow<Unit>

A flow that emits a Unit when the data for the DataStore changes.

Public functions

getVersion

suspend fun getVersion(): Int

Atomically get the current version. 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

suspend fun incrementAndGetVersion(): Int

Atomically increment version and return the new version. 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

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 that returns type T. It guarantees one-at-a-time execution for all the block 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

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 regardless of the attempt result. Pass a boolean to block to indicate if the attempt succeeds. If the attempt fails, block will run immediately after the attempt, without waiting for the lock to become available.

Parameters
block: suspend (Boolean) -> 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

Added in 1.1.0
val updateNotificationsFlow<Unit>

A flow that emits a Unit when the data for the DataStore changes. DataStore collects this flow to signal the action to invalidate cache and re-read data from disk.