2
votes

I've read about new library from Jetpack (now in alpha) - Jetpack Datastore.

It's clear from documentation that it's a sort of Shared Preferences' killer

Jetpack DataStore is a data storage solution that allows you to store key-value pairs or typed objects with protocol buffers

DataStore uses Kotlin coroutines and Flow to store data asynchronously, consistently, and transactionally

If you're currently using SharedPreferences to store data, consider migrating to DataStore instead

If I don't miss anything you couldn't use this library in Java. Am I right? Personally I use Kotlin, but as for me it's a peculiar precedent for AndroidX library.

4

4 Answers

2
votes

January 13, 2021

Version 1.0.0-alpha06 was released. Support for RxJava 2/3 was added, so Datastore can be used in Java now.

Added RxJava wrappers for DataStore. The datastore-rxjava2/3 artifacts contain the wrappers for the core DataStore APIs (RxDataStore, RxDataStoreBuilder, and RxDataMigration). The datastore-preferences-rxjava2/3 artifacts contain a builder to construct a Preferences DataStore.

For that you should add dependencies:

// optional - RxJava2 support
implementation "androidx.datastore:datastore-rxjava2:1.0.0-alpha06"

// optional - RxJava3 support
implementation "androidx.datastore:datastore-rxjava3:1.0.0-alpha06"

In addition now Datastore's official documentation holds code's examples' equivalents for Java.

1
votes

I pretty sure they do not hava plan to do it for java.

Built on Kotlin coroutines and Flow, DataStore provides two different implementations: Proto DataStore, that lets you store typed objects (backed by protocol buffers) and Preferences DataStore, that stores key-value pairs. Data is stored asynchronously, consistently, and transactionally, overcoming most of the drawbacks of SharedPreferences.

Kotlin coroutines/flow are not available to java as far as I am concern. You can read more here, a nice article from Florina.

0
votes

You're right, Jetpack DataStore brings many benefits over the SharedPreference.

Technically you can add it in Java by adding a dependency in gradle.build as usual. But unfortunately, you can't use it because the API exposes suspend functions. So you still need to add a Kotlin support.

It's still in alpha, so maybe they will add Java support. They just need to expose sibling methods for Java, which return something like a Promise.

0
votes

You can use DataStore only in RxJava. In plain java you can use only SharedPreferences now. Let's compare RxJava DataStore Preferences with SharedPreferences

1) access

DataStore:

RxDataStore<Preferences> dataStore =
  new RxPreferenceDataStoreBuilder(context, /*name=*/ "settings").build();

SharedPreferences:

SharedPreferences sharedPref = context.getSharedPreferences(
        getString(R.string.preference_file_key), Context.MODE_PRIVATE);

2) read:

DataStore: define a key for your value (in example for int value) and then access data of datastore : PreferencesKeys.int("example_counter")

datastore.data().map()

data() - access the data of DataStore. This property returns a Flow

map() - returns a Flow which contains the results of applying the given function to each value of the original Flow

SharedPreferences

sharedPref.getInt("highScoreKey", 0);

To retrieve values from a shared preferences file provide the key for the value you want with default value (o here in example)

3) write

DataStore:

dataStore.updateDataAsync()

transactionally update data in a DataStore

SharedPreferences:

Using SharedPreferences.Editor pass the keys and values you want to write with methods such as putInt() and putString(). Then call apply() or commit() to save the changes. Example

Conclusion: developer.android.com suggests consider migrating to DataStore instead of SharedPreferences. But while Java doesn't support DataStore, it's better to use SharedPreferences. if your application uses Kotlin or RxJava - it's better to use DataStore