# Kotlin property delegates for Datastore Preferences library

DevFeed: [Kotlin property delegates for Datastore Preferences library](<https://devfeed.tech/articles/kotlin-property-delegates-for-datastore-preferences-library-26020.md>)

Original publisher: [Read original article](<https://proandroiddev.com/kotlin-property-delegates-for-datastore-preferences-library-5d4e1cdb609b?source=rss-e93d234beac6------2>)

Author: Yahor Urbanovich

Published: 2021-08-23T15:40:08Z

Content type: tutorial

Language: en

Sources: [Stories by Yahor Urbanovich on Medium](<https://devfeed.tech/sources/stories-by-yahor-urbanovich-on-medium.md>)

Topics: [Kotlin](<https://devfeed.tech/topics/kotlin.md>), [Library](<https://devfeed.tech/topics/library.md>), [Coroutines](<https://devfeed.tech/topics/coroutines.md>), [kotlin-flow](<https://devfeed.tech/topics/kotlin-flow.md>), [migration](<https://devfeed.tech/topics/migration.md>)

Tags: [android](<https://devfeed.tech/tags/android.md>), [android-app-development](<https://devfeed.tech/tags/android-app-development.md>), [androiddev](<https://devfeed.tech/tags/androiddev.md>), [coroutines](<https://devfeed.tech/tags/coroutines.md>), [delegates](<https://devfeed.tech/tags/delegates.md>), [delegation](<https://devfeed.tech/tags/delegation.md>), [deprecated](<https://devfeed.tech/tags/deprecated.md>), [flow](<https://devfeed.tech/tags/flow.md>), [kotlin](<https://devfeed.tech/tags/kotlin.md>), [kotlin-flow](<https://devfeed.tech/tags/kotlin-flow.md>), [library](<https://devfeed.tech/tags/library.md>), [migration](<https://devfeed.tech/tags/migration.md>), [property-delegation](<https://devfeed.tech/tags/property-delegation.md>), [suspend-function](<https://devfeed.tech/tags/suspend-function.md>)

## AI overview

This tutorial explains migrating preference storage from SharedPreferences to AndroidX DataStore Preferences. It shows how Kotlin property delegation can encapsulate reading and writing values, and how Flow, coroutines, first(), and runBlocking can support single-value access despite DataStore's Flow-based API.

## Source excerpt

Kotlin delegated property for Datastore Preferences library Recently AndroidX Datastore 1.0 was released. The moment has come, it's time to migrate away from SharedPreferences. As for me, the key benefit is the built-in support of Kotlin Flow and Coroutines. Previously we had to write our own wrapper under SharedPreferences.OnSharedPreferenceChangeListener, emit changes into ConflatedBroadcastChannel and then convert it into Flow. https://medium.com/media/b5410ce9df08d9b73427a2138a6b0a8d/href Firstly, this approach produces a lot of intermediate variables. Secondly, ConflatedBroadcastChannel deprecated and should be replaced with StateFlow. In this case, DataStore comes out as the winner. The migrated to DataStore version looks clean and simple. https://medium.com/media/cce0d485e2af9941e677dc7973a54c6f/hrefRead/write single value using SharedPreferences Let's imagine, we need to store the current App theme as String. For this purpose, we need to initialize SharedPreferences, then make two functions to read and write these values. https://medium.com/media/3bc9760a22ebd1071e4dcb21eaf8d4ba/href Well, two functions it is not so bad, but we can try to merge this into single Property. https://medium.com/media/c3babbf2b6c55bb18172f66ddc825b06/href To make it fully clear and supportable we can use the Kotlin delegation feature. As we want to read and write simultaneously, ReadWriteProperty will suit our goal. https://medium.com/media/80f370a5a5735b911c75d3a0f1d22b21/href Please take a look at the final compact version, where all logic is encapsulated inside the delegate. https://medium.com/media/39b5311b04373b1f31d3030b8b083587/hrefDataStore Preferences and missing API During migration, it turned out that the library doesn't provide an API for reading a single property. DataStore proposes to use Flow for all cases. Developers are now required to read/write the value in the scope of a suspend function. https://medium.com/media/e7c4e45e239e11bc5082f3997e5280dd/href What was e