Using DataStore on Android With Java

40 Views Asked by At

I have been trying to to migrate from SharedPreferences to DataStore. So far I have had no success. The way I had my SharedPreferences implemented before is like this. I have a SharedPreferencesManager that I store my SharedPreferences instance in. The SharedPreferencesManager also handles all my editing and so on for my SharedPreferences. When the Apps Activity goes through OnCreate the SharedPreferencesManager constructor calls this method:

private void createSharedPreferences(Context context)
{
    mSharedPref = context.getSharedPreferences(PREFERENCE_KEY, Context.MODE_PRIVATE);
    int unlock = mSharedPref.getInt(SHARED_PREFERENCES_CREATED, - 1);
    if (unlock == - 1)
    {
        SharedPreferences.Editor editor = mSharedPref.edit();
        editor.putInt(SHARED_PREFERENCES_CREATED, 0);
       //The rest of the default values are excluded for brevity
    }
}

As you can probably see I create the SharedPreferences instance and query if its been created. If not add the default values.

I would like to keep this structure with DataStore but it seem impossible.

I have followed the documentation here. Inserted the dependencies created my DataStore instance like such: mDataStore = new RxPreferenceDataStoreBuilder(context, PREFERENCE_NAME).build();and got a basic POC using the code supplied. I tried to apply it to the structure I have like this it:

private void createDefaultPreferencesValues()
{
    if(!areDefaultsInserted())
    {
        setDefaultValuesAdded(true);
        //Default values excluded for brevity
    }
}

with the areDefaultsInserted() method structure like this:

 public boolean areDefaultsInserted()
{
    return mDataStore.data().map(prefs -> prefs.get(DEFAULT_INSERTED_KEY)).blockingFirst();
}

Initially I got java.lang.NullPointerException: The mapper function returned a null value.. This makes sense since I didnt insert anything into the DataStore. So I tried catching the exception and then finally passing out a false, but still got the The mapper function returned a null value. I tried multiple other ways but the one that caused me the most confusion was this.

private void createDefaultPreferencesValues()
{
   if(mDataStore.data().blockingFirst().asMap().isEmpty())
   {
        setDefaultValuesAdded(true);
       //Values excluded for brevity
    }
}

I can successfully check that the Map is empty but when I then try to assign a value into the DataStore I get the The mapper function returned a null value exception again. Stranger still if I remove the check for the Maps size I can successfully add the default values. Im obviously doing something wrong. How do I catch the null and insert a set of default values. Is it even worth it to use DataStore since it seems like a lot of overhead to store and fetch one literal bit of information? I am not querying Urls on the net or anything. A user can flip a switch and change the value. When they return the value gets fetched and applied to the Ui. Please help. Brain is too smooth :(

Ps. Answers in Java please. The project is Java based

0

There are 0 best solutions below