Android App Development
iOS App Development
Cross Platform App Development
Hire on-demand project developers and turn your idea into working reality.
Big thanks to Webkul and his team for helping get Opencart 3.0.3.7 release ready!
Deniel Kerr
Owner and Founder. Opencart
Top Partners
This is time to say bye bye to SharePreferences. Jetpack DataStore is new solution to store data. It is replacement of SharePreferences. Preferences DataStore is one way of DataStore to save data.
In share preference, we can only save data in key-value. It allows us to store data in key-value pair and in form of object as well.
It saves data using Async Api and it builds with coroutines and flow.
DataStore provides two different implementations :
In this blog, we will cover Preferences DataStore only.
Now, create a instance of datastore. It will create a file name as “MyDataStore”.
“createDataStore” is a extension function created on Context. Method will return DataStore instance.
To save the data, first we need to create a key for our value.
This will define the name of key and it’s type.
If you wanna save int value, you will have to use ‘intPreferencesKey‘.
‘edit‘ method is used to save data in DataStore. It is suspend function. It should be called by either coroutines or another suspend function.
Call a suspend function :
We use Global scope to create coroutines.
Check following link for kotlin coroutines : Kotlin Coroutines
DataStores provides data property to expose the value.
We create string type flow object.
Flow :
To learn more about flow, check following link : Kotlin Flow
Hopefully, this blog will be helpful for you.
Your email address will not be published. Required fields are marked*
Name*
Email*
Save my name email and website in this browser for the next time I comment.
Be the first to comment.
We use cookies to personalize your experience. By continuing to visit this website you agree to our use of cookies. Learn more about privacy policy
Great Product, Great Team, and Great Support Service. And if you want to add more features to the product, they can submit any idea that comes to your mind. They really care about their clients and we are really happy and honored to deal with Webkul.
Osama
USA
India
Global
Name
Email
Enquiry or Requirement
If you have more details or questions, you can reply to the received confirmation email.