DEV Community

Cover image for Unleashing the Power of Jetpack DataStore - Kotlin: Elevate Your Android App’s Data Storage Experience
Manoj Pedvi
Manoj Pedvi

Posted on • Originally published at 10x-programming.com

Unleashing the Power of Jetpack DataStore - Kotlin: Elevate Your Android App’s Data Storage Experience

Introduction

In the realm of Android app development, data storage reigns supreme as a critical aspect that demands meticulous attention. Whether it involves storing user preferences, key-value pairs, or typed objects, embracing a robust and efficient data storage solution is paramount to delivering a seamless user experience. In this opulent blog post, we embark on a journey to explore the unparalleled capabilities of Jetpack DataStore – a lavish data storage library that bestows upon developers a modern and coroutine-based approach to data persistence in Android apps.

Section 1: Understanding Jetpack DataStore

Welcome to the realm of Jetpack DataStore, an exclusive component nestled within the prestigious Android Jetpack library. This elite collection of components and tools has been meticulously curated to empower developers in crafting luxurious Android apps with unparalleled ease. Within this distinguished collection lies DataStore – a regal data storage solution that endows developers with the ability to store key-value pairs or typed objects using protocol buffers. By harnessing the power of Kotlin coroutines and Flow, DataStore offers an opulent experience of asynchronous, consistent, and transactional data storage capabilities.

Section 2: The Two Implementations of DataStore

DataStore showcases its magnificence through two distinct implementations: Preferences DataStore and Proto DataStore.

Preferences DataStore: The Elegance of Simplicity

Enter the world of Preferences DataStore – an embodiment of simplicity and elegance. This refined implementation stands as a paragon of effortless data storage, where data is stored and accessed using keys. Preferences DataStore requires no predefined schema and does not burden developers with the shackles of type safety. It gracefully caters to the storage needs of key-value pairs, such as user preferences and app settings, with utmost grace and sophistication.

To create a Preferences DataStore, use the property delegate created by preferencesDataStore to create an instance of DataStore<Preferences>. Call it once at the top level of your Kotlin file, and access it through this property throughout the rest of your application. This ensures that your DataStore is kept as a singleton.

// At the top level of your Kotlin file:
val Context.dataStore: DataStore<Preferences> by preferencesDataStore(name = "settings")
Enter fullscreen mode Exit fullscreen mode

To read from a Preferences DataStore, use the data property of the DataStore instance. You can expose the stored value as a Flow and use it to retrieve the appropriate value.

val EXAMPLE_COUNTER = intPreferencesKey("example_counter")
val exampleCounterFlow: Flow<Int> = context.dataStore.data
    .map { preferences ->
        // No type safety.
        preferences[EXAMPLE_COUNTER] ?: 0
    }
Enter fullscreen mode Exit fullscreen mode

To write to a Preferences DataStore, use the edit() function provided by Preferences DataStore. This function allows you to transactionally update the data in the DataStore. You can access the current values in the transform block and update them as needed.

suspend fun incrementCounter() {
    context.dataStore.edit { settings ->
        val currentCounterValue = settings[EXAMPLE_COUNTER] ?: 0
        settings[EXAMPLE_COUNTER] = currentCounterValue + 1
    }
}
Enter fullscreen mode Exit fullscreen mode

Proto DataStore: Where Complexity Meets Refinement

Behold the majesty of Proto DataStore – a realm where complexity finds solace in refined sophistication. This distinguished implementation boasts the ability to store data as instances of custom data types, meticulously defined using protocol buffers. Proto DataStore demands a predefined schema, meticulously crafted within proto files, which bestow upon it the power of strong typing and type safety. It serves as the perfect abode for storing intricate and structured data within your app, ensuring an unparalleled level of luxury.

To create a Proto DataStore, there are two steps involved:

  1. Define a class that implements Serializer<T>, where T is the type defined in the proto file. This serializer class tells DataStore how to read and write your data type. Make sure you include a default value for the serializer to be used if there is no file created yet.
object SettingsSerializer : Serializer<Settings> {
    override val defaultValue: Settings = Settings.getDefaultInstance()

    override suspend fun readFrom(input: InputStream): Settings {
        try {
            return Settings.parseFrom(input)
        } catch (exception: InvalidProtocolBufferException) {
            throw CorruptionException("Cannot read proto.", exception)
        }
    }

    override suspend fun writeTo(t: Settings, output: OutputStream) = t.writeTo(output)
}
Enter fullscreen mode Exit fullscreen mode
  1. Use the property delegate created by dataStore to create an instance of DataStore<T>, where T is the type defined in the proto file. Call this once at the top level of your Kotlin file and access it through this property delegate throughout the rest of your app. The filename parameter tells DataStore which file to use to store the data, and the serializer parameter tells DataStore the name of the serializer class defined in step 1.
val Context.settingsDataStore: DataStore<Settings> by dataStore(
    fileName = "settings.pb",
    serializer = SettingsSerializer
)
Enter fullscreen mode Exit fullscreen mode

To read from a Proto DataStore, use the data property of the DataStore instance. You can expose the appropriate property from your stored object as a Flow.

val exampleCounterFlow: Flow<Int> = context.settingsDataStore.data
    .map { settings ->
        // The exampleCounter property is generated from the proto schema.
        settings.exampleCounter
    }
Enter fullscreen mode Exit fullscreen mode

To write to a Proto DataStore, use the updateData() function provided by Proto DataStore. This function transactionally updates a stored object. It gives you the current state of the data as an instance of your data type and updates the data transactionally in an atomic read-write-modify operation.

suspend fun incrementCounter() {
    context.settingsDataStore.updateData { currentSettings ->
        currentSettings.toBuilder()
            .setExampleCounter(currentSettings.exampleCounter + 1)
            .build()
    }
}
Enter fullscreen mode Exit fullscreen mode

Section 3: Best Practices for Using DataStore: Embracing Elegance

To ensure your journey with DataStore remains as opulent as possible, we present a set of best practices to guide you towards a truly luxurious experience:

  1. Embrace Singularity: Never create more than one instance of DataStore for a given file within the same process. Straying from this exclusive principle can shatter the very foundation of DataStore, leading to unexpected complications. Let singularity be the guiding principle to preserve its true essence.
  2. Immutable Grandeur: The generic type employed within DataStore must exude an aura of immutability. Beware the perils of mutation, for they can tarnish the sanctity of DataStore and give rise to treacherous bugs. To maintain the highest standards of luxury, we strongly recommend employing protocol buffers – the epitome of immutability, simplicity, and efficient serialization.
  3. MultiProcessDataStore: For those seeking to access DataStore from multiple processes, indulge in the extravagant offerings of MultiProcessDataStore. Refrain from mingling the usage of SingleProcessDataStore and MultiProcessDataStore for the same file, as such audacity can lead to data inconsistencies and other undesirable consequences.

Section 4: Setting Up DataStore in Your App: A Luxurious Affair

Preparing your Android app for a lavish affair with Jetpack DataStore demands the inclusion of the finest dependencies within your Gradle file. Depending on your preferred implementation – Preferences DataStore or Proto DataStore – make sure to adorn your app’s build.gradle file with the appropriate dependencies.

For Preferences DataStore, indulge in the following splendor:

dependencies {
    implementation "androidx.datastore:datastore-preferences:1.0.0"
    // Optional - Revel in the opulence of RxJava2 support
    implementation "androidx.datastore:datastore-preferences-rxjava2:1.0.0"
    // Optional - Bask in the grandeur of RxJava3 support
    implementation "androidx.datastore:datastore-preferences-rxjava3:1.0.0"
}
Enter fullscreen mode Exit fullscreen mode

For Proto DataStore, immerse yourself in the magnificence of:

dependencies {
    implementation "androidx.datastore:datastore:1.0.0"
    // Optional - Revel in the opulence of RxJava2 support
    implementation "androidx.datastore:datastore-rxjava2:1.0.0"
    // Optional - Bask in the grandeur of RxJava3 support
    implementation "androidx.datastore:datastore-rxjava3:1.0.0"
}
Enter fullscreen mode Exit fullscreen mode

Section 5: Conclusion: A Grand Finale

As we conclude this extravagant journey through the realm of Jetpack DataStore, we invite you to revel in the opulence that awaits. Explore the boundless possibilities of this lavish data storage library, where simplicity and refinement intertwine effortlessly. By embracing the best practices and harnessing the unique features offered by DataStore, you can ensure that your Android apps exude a sense of elegance and luxury in their data storage capabilities. Unleash the power of Jetpack DataStore and elevate your app’s data storage experience to new heights of grandeur.

Top comments (0)