Lilly
08/18/2022, 9:22 PMRussell Stewart
08/18/2022, 9:28 PMLaunchedEffect
might be what you're looking for:
https://developer.android.com/jetpack/compose/side-effectsLilly
08/18/2022, 9:34 PMLandry Norris
08/18/2022, 9:35 PMRussell Stewart
08/18/2022, 9:36 PMLaunchedEffect
.
If you're using Fragments with Compose layouts, then you probably want to look at your Fragment lifecycle methods, like onViewCreated()
or something.Landry Norris
08/18/2022, 9:36 PMLaunchedEffect(Unit)
will run the first time the composition runs.Lilly
08/18/2022, 9:41 PM...Active
or something similiar, I can't remember. This was deprecated by DisposableEffect
.
Aok didn't know. The name does not imply that behaviorwill run the first time the composition runs.LaunchedEffect(Unit)
Landry Norris
08/18/2022, 9:45 PMLilly
08/18/2022, 10:05 PMvm:
var state by mutableStateOf(State.Initial)
fun loadState()
isLoading = true
// do stuff
state = State()
isLoading = false
}
ui:
@Composable
fun Screen() {
LaunchedEffect(Unit) {
vm.loadState()
}
if (!vm.isLoading) {
ScreenContent(vm.State) // shows a list by using state
}
}
When I leave the screen and re-enter it, I can see for a second the list with old state until vm.loadState()
is called and overwrites the old state.
Rendering ScreenContent
is faster than calling vm.loadState()
. At the time ScreenContent is rendered, isLoading is false. It should be true, but call to vm.loadState() is delayed. Any ideas how I can solve it in UI?Ian Lake
08/18/2022, 10:55 PMLilly
08/18/2022, 11:02 PMIf you want to load something only once for the entire existence of the screen, then you need to do that in the initialization of the ViewModelThat's the problem, I want the opposite. State should be reset on every first composition
Ian Lake
08/18/2022, 11:05 PMLilly
08/18/2022, 11:06 PMIan Lake
08/18/2022, 11:07 PMLilly
08/18/2022, 11:09 PMIan Lake
08/18/2022, 11:10 PMLilly
08/18/2022, 11:11 PMIan Lake
08/18/2022, 11:14 PMLilly
08/18/2022, 11:16 PMIsn't that also the case when you are on the screen and you move aroundOur Bluetooth devices don't change their name without interaction, so no. Hmm updating the name might be possible, but why bother with it, when I just need to reload the whole state object
Ian Lake
08/18/2022, 11:17 PMLilly
08/18/2022, 11:20 PMSnapshotStateMap
. In this Map are 200 pairs. I can't update all of these so I simply create a new state object with an empty Map and refill the Map when new data is fetchedIan Lake
08/18/2022, 11:26 PMLilly
08/18/2022, 11:30 PMSnapshotStateMap
? See
Another example where I'm resetting the state uses a. In this Map are 200 pairs. I can't update all of these so I simply create a new state object with an empty Map and refill the Map when new data is fetchedSnapshotStateMap
If I turn the screen off, move to a different location and turn my screen onWhen the listed device is out of range it is wrong to keep it in list. A user would try to connect because the device is in the list but since the user is out of range it is not possible
Ian Lake
08/18/2022, 11:47 PMLilly
08/18/2022, 11:51 PMand you cannot care about stale dataInteresting point of view. Why would it be a problem to reset the state? It isn't really error prone so why isn't it a legit approach for you?
Ian Lake
08/18/2022, 11:56 PMLilly
08/19/2022, 12:00 AMmutableStateMapOf<String, Any>()
. On first composition a one shot vm function which in turn calls a backend operation is called to fetch the data. The Map is then filled with this data (~ 200 pairs). When a bluetooth operation on the device is performed the device is reconnected and I reset the state. Any ideas how to handle this scenario?Ian Lake
08/19/2022, 1:02 AMLilly
08/19/2022, 1:09 AMinit
(like you described) and clear
the Map on re-fetch?Ian Lake
08/19/2022, 2:29 AMLilly
08/21/2022, 3:39 PMStateFlow
coming from domain/backend layer, that holds the status of the backend like CONNECTED, DISCONNECTED
. In Compose it is common to let vm expose the StateFlow
and collectAsState
in composable. Is it also legit to collect the StateFlow
in vm for example in init
with launchIn
to be able to merge the value with my state object? The former approach prohibits the possibility to merge the value with my state object, because the state object is constructed in vm. Or should I never collect a StateFlow
in vm?