How do you fetch an initial value from the databas...
# room
v
How do you fetch an initial value from the database when the viewmodel is first constructed? I'm happy to block the UI, I'm only trying to fetch a single Integer value. Because I can't do `val initialOdometerReading = vehicleRepository.getCurrentOdometer(_selectedVehicle)`because
getCurrentOdometer
is a suspend function (because, I think, all repository functions which call the DB are supposed to be suspend?) Note that I don't want a LiveData result - it's not going to change value suddenly!
If use a mutableState for
initialOdometerReading
and populate it in the
init { }
block - well, that's too late. The screen has been drawn by then, with the wrong value.
m
Don't block the UI thread, instead have a loading/empty state in the UI while you grab the first value
v
Thanks. I eventually worked it out. There's a shift in thinking required for compose which hasn't really sunk in yet. And I find all the examples are based around collections and lists where progressive loading/appearing makes more sense. All I wanted was a single Int from the database before rendering...