https://kotlinlang.org logo
Title
a

AmrJyniat

10/10/2021, 6:21 PM
How to get a boolean value from 
dataStore Preference
 *synchronously*(one-shot) without delay?
o

Orhan Tozan

10/10/2021, 7:58 PM
Use first() on the data flow
l

louiscad

10/11/2021, 6:37 AM
It's not really possible. The best you can do is 👆
a

AmrJyniat

10/11/2021, 7:01 AM
It's very wired, when you replace 
SharedPrefrence
 with 
dataStore
 to optimize the process but actually you miss very basic requirement like this!
l

louiscad

10/11/2021, 8:27 AM
What's your use case for synchronous access to some saved data?
j

Joost Klitsie

10/11/2021, 8:35 AM
@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

Orhan Tozan

10/11/2021, 8:37 AM
@AmrJyniat my answer should cover your usecase. If you want to block the current thread, wrap it with runBlocking {}
a

AmrJyniat

10/11/2021, 8:39 AM
@Orhan Tozan Unfortunately it's not, it's just like normal flow.
@louiscad Showing onBoarding(welcome) screens or skip it.
o

Orhan Tozan

10/11/2021, 8:41 AM
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

louiscad

10/11/2021, 8:41 AM
Showing onBoarding(welcome) screens or skip it.
Well, you can do that asynchronously
a

AmrJyniat

10/11/2021, 8:45 AM
@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

Joost Klitsie

10/11/2021, 8:46 AM
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

AmrJyniat

10/11/2021, 8:48 AM
@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

louiscad

10/11/2021, 8:49 AM
@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

AmrJyniat

10/11/2021, 8:49 AM
@Orhan Tozan I just test it and it takes same time with normal flow🤷‍♂️
l

louiscad

10/11/2021, 8:49 AM
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

Orhan Tozan

10/11/2021, 8:53 AM
@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