I'm pulling my hair out trying to use "async" data...
# getting-started
t
I'm pulling my hair out trying to use "async" datastore stuff. When I access my "special" once-n-only-one preferencesDataStore .data, I get a flow on the data. But what if no preferences have been saved? I want to initialize a startup variable based on its contents. But if non has been saved yet, it just blocks forever. I've tried various incantations of map/latest with or without the orNull adds. Is there no way to get "the current value or something else if there is not yet any value"?
y
I think what you need is stateIn
t
firstOrNull() worked as well. I don't get why lastOrNull would block indefinitely, but firstOrNull() happily returns null right away
y
to find out the
last
thing, you need to collect everything first. It is strange that
firstOrNull
works though
Careful btw because
firstOrNull
is a terminal operator
t
what do you mean that it's a terminal operator? I find that I can then update the pref's value to have a value, and use the same flow object (not create a new one) and ask it firstOrNull() and it now returns the newly updated value.
y
That kind of makes sense, I guess. That's likely because the
flow
given to you by the library is a shared one, and hence the terminal operator doesn't actually result in termination.
t
I can't say I love "flow" programming. But I hope I'll get it. Maybe then I'll be awed by it
d
It's possible you're using
flow
in a way it's not actually intended. It sounds like this particular case you want to use MutableStateFlow.
t
The PreferencesDataStore defines the type of Flow I get , i think?
d
I see. I’m not familiar with that api.