Hello. I am facing an issue where a StateFlow value resets back to its original value even after updating it. This is only happening in iOS.
The following code is in the commonMain of the shared module
Copy code
private val _isLoading: MutableStateFlow<Boolean> = MutableStateFlow(false)
internal val isLoading: StateFlow<Boolean> = _isLoading
The log that gets printed is
Loading: false
Loading: true
Loading: false
This works perfectly fine in the android app. All the code exists in the shared module. Any help would be appreciated.
Thank you
👍 1
p
Pablichjenkov
06/07/2023, 5:18 PM
Can you print the ViewModel instance. I think App is getting recompose and is creating a new instance
Pablichjenkov
06/07/2023, 5:21 PM
Now the reason why you don't see the false/true logs twice is because key = true in remember, try key = ViewModel. And also host the ViewModel outside, in the parent class instance or parent composable and pass just the reference to the composable function
Pablichjenkov
06/07/2023, 5:24 PM
You don't see it in Android perhaps because some mechanics under setContent {} are not causing the App recomposition. Perhaps is more optimized in that sense
k
KimJason
06/08/2023, 1:10 AM
@Manpreet Kunnath
Copy code
val vm = remember { SomeViewModel() }
add remember!
m
Manpreet Kunnath
06/08/2023, 4:50 AM
I'll do this and check. I also think it's related to recomposition and remember. Thank you.