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

about 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 counsel on the subject of the world. admittance slowly for that cause you comprehend competently and appropriately. will progress your data effectively and reliably


The picture is taken from dreamstime.com

Introduction

FTo begin with, let’s clarify why I want the Cookie storage. To establish the person, the server units JWT token in cookies. And all requests from the cellular 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 carried out. Because the challenge has KMM modules (at the moment 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.consumer.plugins.cookies.CookiesStorage). That is how I carried out it.

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

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

however lately Android workforce introduced the KMM model of Knowledge warehouse. For me it’s a silver bullet, because it permits me to:

  1. Migrate from the shared preferences a information warehouse (You’ll be able to learn its benefits on this sensible article)
  2. Have the identical cross-platform code as a substitute of anticipated precise lessons

At the moment, 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’ll speak later). Please word that the variations are totally different.

Level two.

I created a separate repository for the Knowledge warehouse. right here is your listing

What do we have now right here?

  • To begin with, now the repository is in a typical module, as a substitute of the Android and iOS components. Class DataStoreRepository implements DataStoreRepository from the area layer to attain the precept of dependency inversion and make the category simple to check.
  • Second, as a substitute of say container (What do we have to get the shared preferences), now we go the information warehouse instantly. So conceptually nothing adjustments.
  • Third: the strategies for getting and saving values ​​had 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. 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’s also suspended, the getString technique turned suspended.

Get the worth of the PreferencesDataStore is similar to working with shared preferences, however as a substitute of strategies getString, getInt and so forth, now we use PreferencesKeys (the complete listing of them could be discovered right here).

Y CookiesStorage nearly doesn’t change. The one distinction is that as a substitute of PreferencesRepository now go the Knowledge warehouse repository. However they’re most likely the identical entities.

Virtually the final…

And right here we’re confronted with a small drawback: the way to migrate the info, which is already saved within the shared preferences a Knowledge warehouse. Fortunately, it is easy. Once we instantiate the information warehousewe will go the listing 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 seems like:

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

within SharedPreferencesMigration we have now to go the shared preferences object or context and preferences identify: to permit the operate to get the shared preferences object below the hood. We are able to additionally specify the keys we need to migrate because the third parameter.

Underneath the hood, the migration operate seems easy, we simply learn all of the values ​​from shared preferencesthen filter solely the values ​​we’d like and save them in Preferences.

Conclusion.

  1. look of this information warehouse in KMM It’s another 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 a substitute of anticipate/precise.
  3. With the assistance of Circulate, we will observe the adjustments of storage.
  4. For extra complicated 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 various to settings library.
  6. It has the migrations mechanism.

You could find the supply code on Github.

Thanks for studying! Be at liberty to ask questions and depart suggestions in feedback or LinkedIn.

I want the article not fairly My expertise of migration CookiesStorage from SharedPreferences to DataStore in Android A part of the KMM challenge | by Sergei Mikhailovskii | Nov, 2022 provides keenness to you and is beneficial for surcharge to your data

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

News

Good day Fediverse! Introducing Buffer for Mastodon | Origin Tech

roughly Good day Fediverse! Introducing Buffer for Mastodon will lid the newest and most present instruction roughly the world. entrance slowly appropriately you perceive with out issue and appropriately. will addition your information proficiently and reliably Mastodon is at present going by way of an explosive section of progress. Some folks say it reminds them […]

Read More
News

Samsung T7 Defend 4TB is Now Out there | Summary Tech

roughly Samsung T7 Defend 4TB is Now Out there will cowl the newest and most present counsel regarding the world. learn slowly fittingly you comprehend capably and accurately. will progress your information nicely and reliably Samsung had some thrilling information on the stable state drive (SSD) entrance at present. The corporate introduced the provision of […]

Read More
News

What’s HelloFresh and the way does it work? | Gamer Tech

just about What’s HelloFresh and the way does it work? will lid the newest and most present advice roughly the world. means in slowly thus you comprehend skillfully and accurately. will buildup your data skillfully and reliably Edgar Cervantes / Android Authority Regardless of being one in every of life’s best pleasures, meals will also […]

Read More
x