How to get a boolean value from `dataStore Prefere...
# coroutines
a
How to get a boolean value from 
dataStore Preference
 *synchronously*(one-shot) without delay?
o
Use first() on the data flow
l
It's not really possible. The best you can do is 👆
a
It's very wired, when you replace 
SharedPrefrence
 with 
dataStore
 to optimize the process but actually you miss very basic requirement like this!
l
What's your use case for synchronous access to some saved data?
j
@AmrJyniat the problem is that the shared preferences read from internal storage, blocking the main thread while doing it as it is IO. This is a flaw and you should anyway already have used an asynchronous way if you wished to do it correctly (I don't do it but I know I should 😄 ) The data store is there to fix this problem and correctly return a flow that doesn't block the main thread. You have to find a way to do this and adjust your app and perhaps some of the architecture to handle it, coming from a synchronous way and transform it in an asynchronous way is not a trivial task. I for example in a private app return a result (Loading, Error, Success) and while the data "am I signed on or not" is being loaded from the DataStore I show a start screen. ORRR (don't try this at home) wrap the
first()
in a
runBlocking {}
and mess up your apps (startup) performance.
💡 1
but as with the good old preferences, there is no way to do it without delay. The preferences just masked it
o
@AmrJyniat my answer should cover your usecase. If you want to block the current thread, wrap it with runBlocking {}
a
@Orhan Tozan Unfortunately it's not, it's just like normal flow.
@louiscad Showing onBoarding(welcome) screens or skip it.
o
Can you elaborate? You asked a way to be able to get data in a one-shot, which is what my answer gives you.
@AmrJyniat
l
Showing onBoarding(welcome) screens or skip it.
Well, you can do that asynchronously
a
@louiscad nope, if do it asynchronously and you need to skip welcome screen so it will be shown for seconds then will skip it.
j
you should show a splash screen while loading the information
and then decide if you transition to a welcome or some other screen
👍 1
a
@Joost Klitsie Thanks for the explanation it's helpful, so when I use SharedPref then will take the same time that datastore takes but with a frozen UI thread? is this right?
l
@AmrJyniat Look for the new androidx.splashscreen library if not showing the main UI for milliseconds (or seconds if the device is insanely slow, which would get your app killed anyway if it lasts 5 seconds), if showing it briefly is an issue.
a
@Orhan Tozan I just test it and it takes same time with normal flow🤷‍♂️
l
By killed, I mean ANR/freeze, but since Android 9 IIRC, if the freeze lasts 5 seconds, the system is more likely to just kill the app.
o
@AmrJyniat you're right, it takes same time of old sharedprefs and new data store. Also, if you dont want it to block the ui thread, just launch it in a new coroutine, even better