lesincs
12/16/2022, 7:57 AMlesincs
12/16/2022, 7:58 AMLaunchEffect(Unit) {
viewModel.getDataForScreenB()
}
it works fine until I pop back to screen B from screen C, due to the feature of LaunchEffect
, viewModel.getDataForScreenB()
would also be invoked once, which is not I want.
So I was wondering is there any elegant way to handle this? I’ve come up with several ways, like: putting the request into some place where I navigate from A to B or every time call getDataForScreenB()
, check if there is already some data, if so just return.
But I thought either of them passes for “elegant”, so any ideas?dorche
12/16/2022, 2:22 PMefemoney
12/16/2022, 3:21 PMDeferred
of the data in your VM like
val bData = myScope.async(CoroutineStart.LAZY) { ... }
and your getDataForScreenB()
will be a simple function that await()
’s the data. If it already exists in VM, good it’ll return it, if not, it will suspend until it is available.lesincs
12/19/2022, 11:22 AM