8

I can only find ways to implement datastore through Kotlin. I have tried creating it with DataStore<Preferences> datastore = new Datastore<Preferences> but as soon as proceed with it, it overrides to methods namely save and loadData but the parameters passed in them are also in Kotlin. Should I proceed with Sharedpreferences only?

Vadim Kotov
  • 8,084
  • 8
  • 48
  • 62
Prabhav Sharma
  • 108
  • 1
  • 7
  • Please read [How to ask](https://stackoverflow.com/help/how-to-ask) and update your question with recommendations. – fartem Feb 05 '21 at 16:58

2 Answers2

5

There are few steps to implement dataStore in java base.

First and foremost, it is good to notice that there are 2 different types of dependencies for the datastore.

  1. TYPED datastore
  2. Preferences DataStore (SharedPreferences like APIs)

Here are some steps to implement the latter one in the java based application.

1. Implementation

// Preferences DataStore (SharedPreferences like APIs)
dependencies {
  implementation "androidx.datastore:datastore-preferences:1.0.0-alpha06"

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

2. Create a Preferences DataStore

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

3. Write to a Preferences DataStore

Single<Preferences> updateResult =  RxDataStore.updateDataAsync(dataStore, 
prefsIn -> {
  MutablePreferences mutablePreferences = prefsIn.toMutablePreferences();
  Integer currentInt = prefsIn.get(INTEGER_KEY);
  mutablePreferences.set(INTEGER_KEY, currentInt != null ? currentInt + 1 : 1);
  return Single.just(mutablePreferences);
});

// The update is completed once updateResult is completed.

3. Read from a Preferences DataStore

Preferences.Key<Integer> EXAMPLE_COUNTER = PreferencesKeys.int("example_counter");

Flowable<Integer> exampleCounterFlow =
  RxDataStore.data(dataStore).map(prefs -> prefs.get(EXAMPLE_COUNTER));

if you want to do more complex please checkout the full documentation

2

I am new to posting here, so I can not add a comment, but I want to build on the answer from Mehran. Also the answer is old, so I do not know if this worked like that, but I noticed several things:

Now, you dont have to implement the alpha versions:

//DataStore instead of SharedPreferences
implementation "androidx.datastore:datastore-preferences:1.0.0"
//RxJava2 support
implementation "androidx.datastore:datastore-preferences-rxjava2:1.0.0"

Furthermore, If you want to use the RxDataStore and the associated RxDataStoreBuilder the simple DataStore doesnt work:

RxDataStore<Preferences> dataStoreRX;
dataStoreRX = new RxPreferenceDataStoreBuilder(this,"datastorename").build();

To get data from the DataStore you can use:

value.blockingGet();

But there is way more to that. If you want to use the DataStore across several activities you need to use a Singleton. The reason is you cant create more than one instance of one DataStore. That is in the documentation but I think it is not complete. Also error-handling is not described there.

I elaborate more on this here: Link to Medium

A little shame with the plug, but one shall not start a question here if there is no actual question. So I hope this helps people who want to use it.

maxvolk
  • 31
  • 3