# StorageConnection

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/StorageConnection.kt+class:androidx.datastore.core.StorageConnection) Added in [1.1.0](https://developer.android.com/jetpack/androidx/releases/datastore#1.1.0)

*** ** * ** ***

Common/AllAndroid/JVMNative/C/iOSJavaScript
- Cmn

  ```
  interface StorageConnection<T : Any?> : Closeable
  ```

*** ** * ** ***

StorageConnection provides a way to read and write a particular type of data. StorageConnections are created from `https://developer.android.com/reference/kotlin/androidx/datastore/core/Storage` objects.

## Summary

| ### Public functions |
|---|---|---|
| `suspend R` | `<R : https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-any/index.html?> https://developer.android.com/reference/kotlin/androidx/datastore/core/StorageConnection#readScope(kotlin.coroutines.SuspendFunction2)(block: suspend https://developer.android.com/reference/kotlin/androidx/datastore/core/ReadScope<T>.(locked: https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-boolean/index.html) -> R)` Creates a scope for reading to allow storage reads, and will try to obtain a read lock. | Cmn |
| `suspend https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-unit/index.html` | `https://developer.android.com/reference/kotlin/androidx/datastore/core/StorageConnection#writeScope(kotlin.coroutines.SuspendFunction1)(block: suspend https://developer.android.com/reference/kotlin/androidx/datastore/core/WriteScope<T>.() -> https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-unit/index.html)` Creates a write scope that guaranteed to only have one single writer, ensuring also that any reads within this scope have the most current data. | Cmn |

| ### Public properties |
|---|---|---|
| `https://developer.android.com/reference/kotlin/androidx/datastore/core/InterProcessCoordinator` | `https://developer.android.com/reference/kotlin/androidx/datastore/core/StorageConnection#coordinator()` Provides a coordinator to guarantee data consistency across multiple threads and processes. | Cmn |

| ### Extension functions |
|---|---|---|
| `suspend T` | `<T : https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-any/index.html?> https://developer.android.com/reference/kotlin/androidx/datastore/core/StorageConnection<T>.https://developer.android.com/reference/kotlin/androidx/datastore/core/StorageConnection#(androidx.datastore.core.StorageConnection).readData()()` | Cmn |
| `suspend https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-unit/index.html` | `<T : https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-any/index.html?> https://developer.android.com/reference/kotlin/androidx/datastore/core/StorageConnection<T>.https://developer.android.com/reference/kotlin/androidx/datastore/core/StorageConnection#(androidx.datastore.core.StorageConnection).writeData(kotlin.Any)(value: T)` | Cmn |

| ### Inherited functions |
|---|
| From [androidx.datastore.core.Closeable](https://developer.android.com/reference/kotlin/androidx/datastore/core/Closeable) |---|---|---| | `https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-unit/index.html` | `https://developer.android.com/reference/kotlin/androidx/datastore/core/Closeable#close()()` Closes the specified resource. | Cmn | |

## Public functions

### readScope

Cmn

```
suspend fun <R : Any?> readScope(block: suspend ReadScope<T>.(locked: Boolean) -> R): R
```

Creates a scope for reading to allow storage reads, and will try to obtain a read lock.

| Parameters |
|---|---|
| `block: suspend https://developer.android.com/reference/kotlin/androidx/datastore/core/ReadScope<T>.(locked: https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-boolean/index.html) -> R` | The block of code that is performed within this scope. Block will receive `locked` parameter which is true if the try lock succeeded. |

| Throws |
|---|---|
| `https://developer.android.com/reference/kotlin/androidx/datastore/core/IOException` | when there is an unrecoverable exception in reading. |

### writeScope

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

```
suspend fun writeScope(block: suspend WriteScope<T>.() -> Unit): Unit
```

Creates a write scope that guaranteed to only have one single writer, ensuring also that any reads within this scope have the most current data.

| Throws |
|---|---|
| `https://developer.android.com/reference/kotlin/androidx/datastore/core/IOException` | when there is an unrecoverable exception in writing. |

## Public properties

### coordinator

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

```
val coordinator: InterProcessCoordinator
```

Provides a coordinator to guarantee data consistency across multiple threads and processes.

## Extension functions

### StorageConnection.readData

Cmn 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/StorageConnection.kt+function:readData)

```
suspend fun <T : Any?> StorageConnection<T>.readData(): T
```

### StorageConnection.writeData

Cmn 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/StorageConnection.kt+function:writeData)

```
suspend fun <T : Any?> StorageConnection<T>.writeData(value: T): Unit
```