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

virtually My expertise of migration CookiesStorage from SharedPreferences to DataStore in Android A part of the KMM challenge | by Sergei Mikhailovskii | Nov, 2022 will cowl the most recent and most present suggestion not far off from the world. go surfing slowly because of this you perceive with out problem and accurately. will deposit your information dexterously 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 have to 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 challenge has KMM modules (at present supported solely Android half), for the community I take advantage of 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.shopper.plugins.cookies.CookiesStorage). That is how I applied it.

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

And right here we discover the principle downside. Since SharedPreferences is barely supported in AndroidSo, we now have to make use of the true expectation mechanism of KMM and write implementations in iOS Y Android.

however not too long ago Android staff 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 knowledge warehouse (You may learn its benefits on this sensible article)
  2. Have the identical cross-platform code as an alternative of anticipated precise lessons

Presently, the largest downside 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 knowledge 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 accommodates the Shared preferences migration — We’ll discuss later). Please word that the variations are completely different.

Level two.

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

What do we now have right here?

  • To begin with, now the repository is in a standard module, as an alternative of the Android and iOS elements. Class DataStoreRepository implements DataStoreRepository from the area layer to realize 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 knowledge warehouse straight. So conceptually nothing modifications.
  • Third: the strategies for getting and saving values ​​have been discontinued, since they knowledge warehouse enhancing (within the case of learn values) is suspended. What about studying the values? I made a decision to not come again. Circulate, as a result of within the present implementation I execute the tactic on each request, so I do not want the watch mechanism. It is sufficient to use first or nullhowever since it is usually suspended, the getString methodology grew to become 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 complete record of them might 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 downside: the way to migrate the information, which is already saved within the shared preferences a Information warehouse. Fortunately, it is easy. After we instantiate the knowledge warehousewe are able to cross the record of migrations (bear in mind to have just one occasion of knowledge 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 seems to be like:

Second, that is the way in which I create the record of migrations:

inside SharedPreferencesMigration we now have to cross the shared preferences object or context and preferences identify: to permit the perform to get the shared preferences object below the hood. We are able to additionally specify the keys we wish to migrate because the third parameter.

Beneath the hood, the migration perform seems to be 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 knowledge warehouse in KMM It’s yet one more level to look to the aspect of the KMM for Android builders because it permits us to make use of extra acquainted lessons and strategies on all platforms. However word that the library is in dev model.
  2. It permits us to make use of extra frequent code as an alternative of anticipate/precise.
  3. With the assistance of Circulate, we are able to observe the modifications of storage.
  4. For extra advanced conditions (than retaining key-value) you need to use ProtoDataStore(the hyperlinks are right here Y right here).
  5. knowledge warehouse is a extra highly effective various to settings library.
  6. It has the migrations mechanism.

You could find 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 challenge | by Sergei Mikhailovskii | Nov, 2022 provides notion to you and is beneficial for further to your information

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