Preferences DataStore in 10min (SharedPreferences deprecated)

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
hey guys welcome back to a new video in this video i'm going to show you how you can use the new preferences data store which is the replacement for shared preferences because chat preferences got deprecated in the latest api version so this is essentially just a new way of saving preferences in your app so what we will do here we can enter a key so for example android enter a value um hey guys for example we can save this key and when we now enter this key again and we click on read then we will simply read the value of whatever we saved under this specific key here so if we enter something else here and click on read then no value found so there must be something saved here so essentially there are two kind of versions of data store you can use the one hand is preferences data store which we will use here and the other kind is proto datastore so essentially they are very similar but protodatastore is also typesafe preferences datastore is not but for proto-data store you need to set up a lot more you need to write your your model your schema in such a pro-above language and that's in my opinion just a little bit too much to just save some preferences usually you know the type of your preferences so you don't really need that type safety and also usually in apps you have a roon database or so implemented so that you could just save your preferences in that database if you really need that type safety so i will show you this preferences data store here which is very similar to shared preferences just that it uses kotlin flows so everything will now happen asynchronously with shared preferences i think reading was synchronous so there was no way to execute that on a background thread even though that should happen so this is now definitely better with datastore so make sure to get this initial project here with this activity main layout and these gradle dependencies here from this video's description from my github repository here and as you can see i also just set up view binding so that we don't need to worry about that in this video because this video is about data store so to actually start that is very similar to shared preferences we essentially want to create a datastore object here so that will be a private latent var datastore which is of type datastore and here we specify the type of datastore we use the preferences datastore and we need to make sure to choose this preferences from the datastore library and here in oncreate we can then assign that datastore latent var to simply create data store that created a store function here we need to import that or we don't um do we yes we do this is essentially just an extension function for context objects and because we're inside of an activity here that is no problem because we already refer to context here but if you would need to call that function from outside of an activity then you would need to write something like context dot create data store and here we now could pass a bunch of parameters but we essentially only want to pass a name and we also needed to do that for shed preferences so it's very similar so for example just settings you really shouldn't save any complex data here inside of datastore um same for shared preferences for that you should uh rather use the room database but if you have something like settings then that is really suited well for that and now we want to create two functions one function to save something into our datastore instance here and one function to read from it so we will have a private suspend function here so as i said data store is based on kotlin flow so it uses qrutins to read and write data so private suspend function save and that will take a key so we will save our data in key value pairs exactly as we did for shared preferences and we specify the value which i choose a string here for but you can also save integers booleans and all that stuff so the first thing we want to do here is we want to specify a key so you might think we already have a key but data store preferences uses some special kind of keys but we can create that with our string key here so we write val datastore key and that is equal to a new preferences key of type string and we pass our key here so that will just as you can see create such a preferences.key which we need to write something into our datastore instance and now we can already do that since that is also very similar to sharedpreferences we just use datastore dot edit and you can see in here we have a reference to immutable preferences and that also is this pen function as you can see here at that icon so that is executed inside of a curtain here which is fine because that is a suspense function here so in here we can now use those mutable preferences which we can essentially give a name of settings so settings add our data store key so we can just use that as if it was a map here and we set that to our value and that is it that's how we write to preferences data store and now we can do the same for our read function we can actually just copy this save function here paste it below call that read if we want to read something we only need the key because we don't know the value yet but this will return a string here that is nullable so that is the value that we read from that key and if it does not exist we will simply return null here and then we also need this data store key just to read from our data store but we don't need this instead we can now get the preferences so from which we can read our actual values from datastore.data you can see that data now returns a flow of preferences and because that flow essentially just emits that single preferences object we can use that first so that will return that first emission of that flow and that will be our preferences object from which we can now read data so we can simply return preferences at the index of our data store key here and as i said this solution here with preference data store is not type save so we don't really know here if this is a string if this is an integer or so well we know it because we saved the string at that specific key but this essentially just returns any so if if this would if you would have saved an integer here at that key but we would return a string here then our program would simply crash here but usually we know what we save here and that just makes sense so in oncreate then we want to assign an onclicklistener to our button save so then we want to essentially save the data from our to edittext to datastore so the key and the value and because we need to execute that from a crew team we want to launch that in lifecycle scope so in here we call safe the key is binding dot et save key dot text that tostring and the values et save value dot that text.tostring so that's it how we save something and now we want to assign an unclick listener to our button read also launch a curtin here and here we want to simply set the text of our tv read value to whatever we essentially got from this read function so we could save that in a variable here val value is equal to read we want to read what at the key whatever is in binding.et read key dot text dot tostring and then we can set the text here to value and if that is now we set it to no value found and that is it we can now launch our app take a look here we can enter a key hello what is up we click on save we enter the key again and click on read and we get what is up and even if we close the app and open it again here then we can still access the data we saved at that key so if we now enter hello again click on read then you can see we get what is up again so if you really like this video please give it a like comment below if you will use this data store in future in your project and also hit the subscribe button and don't miss out on regular android tutorials every second day if you're interested in more advanced android courses then check out the first link in this video's description that you will get to my website and since today is black friday there is still an offer going where you get two of my premium courses in a bundle for the price of only one so if i upload this video this should last 12 more hours and then the offer will be gone so just check it out and see if it is something for you have an awesome day see you next video bye [Music] [Music] you
Info
Channel: Philipp Lackner
Views: 13,207
Rating: 4.9607844 out of 5
Keywords: android, tutorial, philip, philipp, filipp, filip, fillip, fillipp, phillipp, phillip, lackener, leckener, leckner, lackner, kotlin, mobile, datastore, android studio, shared preferences
Id: McnVx7l5awk
Channel Id: undefined
Length: 11min 13sec (673 seconds)
Published: Fri Nov 27 2020
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.