My expertise of migration CookiesStorage from SharedPreferences to DataStore in Android A part of the KMM venture | by Sergei Mikhailovskii | Nov, 2022 | Tech Prism

not fairly My expertise of migration CookiesStorage from SharedPreferences to DataStore in Android A part of the KMM venture | by Sergei Mikhailovskii | Nov, 2022 will lid the newest and most present instruction on the world. proper of entry slowly appropriately you perceive skillfully and accurately. will accumulation your information nicely and reliably

The picture is taken from


FTo begin with, let’s clarify why I would like the Cookie storage. To establish the person, the server units JWT token in cookies. And all requests from the cell app should be despatched with this cookie. When the person logs out of the app, the token cookie is eliminated because the session ends.

Now we’re going to clarify how the earlier CookiesStorage It was applied. Because the venture has KMM modules (at present supported solely Android half), for the community I exploit the Ktor library. It permits us to put in the Http Cookies plug.

what pertains to CookiesStorageis the subclass of Ktor Cookie Storage (io.ktor.consumer.plugins.cookies.CookiesStorage). That is how I applied it.

As you may see, the logic is sort of easy. When the server units the cookie, the system checks if this cookie is suitable with the applying (exists in Cookie kind), we reserve it to native storage (in case Android the worth is saved in shared preferences).

And right here we discover the principle drawback. Since SharedPreferences is barely supported in AndroidSo, we’ve to make use of the actual expectation mechanism of KMM and write implementations in iOS Y Android.

however not too long ago Android crew introduced the KMM model of Information warehouse. For me it’s a silver bullet, because it permits me to:

  1. Migrate from the shared preferences a information warehouse (You possibly can learn its benefits on this good article)
  2. Have the identical cross-platform code as an alternative of anticipated precise courses

At present, the largest disadvantage is that the library is in developer stage, however when did it cease the builders :). So now let me clarify, how did I migrate to the information warehouse.

Level one.

We have to add the dependencies: androidx.datastore:datastore-preferences-core:1.1.0-dev01 (partly shared) and androidx.datastore:datastore preferences:1.1.0-alpha01 (because it comprises the Shared preferences migration — We are going to speak later). Please word that the variations are completely different.

Level two.

I created a separate repository for the Information warehouse. right here is your checklist

What do we’ve right here?

  • To begin with, now the repository is in a typical module, as an alternative of the Android and iOS elements. Class DataStoreRepository implements DataStoreRepository from the area layer to attain the precept of dependency inversion and make the category simple to check.
  • Second, as an alternative of say container (What do we have to get the shared preferences), now we cross the information warehouse straight. So conceptually nothing modifications.
  • Third: the strategies for getting and saving values ​​have been discontinued, since they information warehouse enhancing (within the case of learn values) is suspended. What about studying the values? I made a decision to not come again. Stream, as a result of within the present implementation I execute the strategy on each request, so I do not want the watch mechanism. It is sufficient to use first or nullhowever since additionally it is suspended, the getString methodology turned suspended.

Get the worth of the PreferencesDataStore is similar to working with shared preferences, however as an alternative of strategies getString, getInt and so forth, now we use PreferencesKeys (the total checklist of them may be discovered right here).

Y CookiesStorage nearly doesn’t change. The one distinction is that as an alternative of PreferencesRepository now cross the Information warehouse repository. However they’re most likely the identical entities.

Virtually the final…

And right here we’re confronted with a small drawback: methods to migrate the information, which is already saved within the shared preferences a Information warehouse. Fortunately, it is simple. Once we instantiate the information warehousewe will cross the checklist of migrations (keep in mind to have just one occasion of information warehouse in the identical course of to keep away from accessing the file concurrently and catching the exception).

First: That is what the DataStore creation appears like:

Second, that is the best way I create the checklist of migrations:

within SharedPreferencesMigration we’ve to cross the shared preferences object or context and preferences identify: to permit the perform to get the shared preferences object underneath the hood. We will additionally specify the keys we need to migrate because the third parameter.

Below the hood, the migration perform appears easy, we simply learn all of the values ​​from shared preferencesthen filter solely the values ​​we want and save them in Preferences.


  1. look of this information warehouse in KMM It’s yet one more level to look to the facet of the KMM for Android builders because it permits us to make use of extra acquainted courses and strategies on all platforms. However word that the library is in dev model.
  2. It permits us to make use of extra widespread code as an alternative of count on/precise.
  3. With the assistance of Stream, we will observe the modifications of storage.
  4. For extra advanced conditions (than maintaining key-value) you need to use ProtoDataStore(the hyperlinks are right here Y right here).
  5. information warehouse is a extra highly effective different to settings library.
  6. It has the migrations mechanism.

You will discover the supply code on Github.

Thanks for studying! Be happy to ask questions and go away suggestions in feedback or LinkedIn.

I hope the article nearly My expertise of migration CookiesStorage from SharedPreferences to DataStore in Android A part of the KMM venture | by Sergei Mikhailovskii | Nov, 2022 provides perspicacity to you and is helpful for calculation to your information

My experience of migration CookiesStorage from SharedPreferences to DataStore in Android Part of the KMM project | by Sergei Mikhailovskii | Nov, 2022