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

*** ** * ** ***


```
public interface StorageConnection<T extends Object> extends Closeable
```

<br />

*** ** * ** ***

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

## Summary

| ### Public methods |
|---|---|
| `abstract @https://developer.android.com/reference/androidx/annotation/NonNull https://developer.android.com/reference/androidx/datastore/core/InterProcessCoordinator` | `https://developer.android.com/reference/androidx/datastore/core/StorageConnection#getCoordinator()()` Provides a coordinator to guarantee data consistency across multiple threads and processes. |
| `abstract @https://developer.android.com/reference/androidx/annotation/NonNull R` | `<R extends https://developer.android.com/reference/java/lang/Object.html> https://developer.android.com/reference/androidx/datastore/core/StorageConnection#readScope(kotlin.coroutines.SuspendFunction2)( @https://developer.android.com/reference/androidx/annotation/NonNull SuspendFunction2<@https://developer.android.com/reference/androidx/annotation/NonNull https://developer.android.com/reference/androidx/datastore/core/ReadScope<@https://developer.android.com/reference/androidx/annotation/NonNull T>, @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 R> block )` Creates a scope for reading to allow storage reads, and will try to obtain a read lock. |
| `abstract void` | `https://developer.android.com/reference/androidx/datastore/core/StorageConnection#writeScope(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/androidx/datastore/core/WriteScope<@https://developer.android.com/reference/androidx/annotation/NonNull T>, https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-unit/index.html> block )` 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. |

| ### Extension functions |
|---|---|
| `default final @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/StorageConnectionKt.https://developer.android.com/reference/androidx/datastore/core/StorageConnection#(androidx.datastore.core.StorageConnection).readData()( @https://developer.android.com/reference/androidx/annotation/NonNull https://developer.android.com/reference/androidx/datastore/core/StorageConnection<@https://developer.android.com/reference/androidx/annotation/NonNull T> receiver )` |
| `default final void` | `<T extends https://developer.android.com/reference/java/lang/Object.html> https://developer.android.com/reference/androidx/datastore/core/StorageConnectionKt.https://developer.android.com/reference/androidx/datastore/core/StorageConnection#(androidx.datastore.core.StorageConnection).writeData(kotlin.Any)( @https://developer.android.com/reference/androidx/annotation/NonNull https://developer.android.com/reference/androidx/datastore/core/StorageConnection<@https://developer.android.com/reference/androidx/annotation/NonNull T> receiver, @https://developer.android.com/reference/androidx/annotation/NonNull T value )` |

| ### Inherited methods |
|---|
| From [androidx.datastore.core.Closeable](https://developer.android.com/reference/androidx/datastore/core/Closeable) |---|---| | `abstract void` | `https://developer.android.com/reference/androidx/datastore/core/Closeable#close()()` Closes the specified resource. | |

## Public methods

### getCoordinator

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

```
abstract @NonNull InterProcessCoordinator getCoordinator()
```

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

### readScope

```
abstract @NonNull R <R extends Object> readScope(
    @NonNull SuspendFunction2<@NonNull ReadScope<@NonNull T>, @NonNull Boolean, @NonNull R> block
)
```

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

| Parameters |
|---|---|
| `@https://developer.android.com/reference/androidx/annotation/NonNull SuspendFunction2<@https://developer.android.com/reference/androidx/annotation/NonNull https://developer.android.com/reference/androidx/datastore/core/ReadScope<@https://developer.android.com/reference/androidx/annotation/NonNull T>, @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 R> block` | 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/androidx/datastore/core/IOException` | when there is an unrecoverable exception in reading. |

### writeScope

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

```
abstract void writeScope(
    @NonNull SuspendFunction1<@NonNull WriteScope<@NonNull T>, Unit> block
)
```

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/androidx/datastore/core/IOException` | when there is an unrecoverable exception in writing. |

## Extension functions

### StorageConnectionKt.readData

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)

```
default final @NonNull T <T extends Object> StorageConnectionKt.readData(
    @NonNull StorageConnection<@NonNull T> receiver
)
```

### StorageConnectionKt.writeData

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)

```
default final void <T extends Object> StorageConnectionKt.writeData(
    @NonNull StorageConnection<@NonNull T> receiver,
    @NonNull T value
)
```