1 year ago
#329518

Satyam Gondhale
App Crash when observing a Live Data in Jetpack Compose Composable due to memory issue
I have one Composable
which has one Callback
function which is responsible to perform some action based on value it is observing in ViewModel
. The Composable looks like.
@Composable
fun MyComposable(
viewModel: MyViewModel = hiltViewModel(),
navigateToSecondScreen: () -> Unit
) {
val stateFlow by viewModel._screenState.collectAsState()
val apiSuccess by viewModel.apiCallSuccess.observeAsState(initial = false)
if(apiSuccess){
navigateToSecondScreen()
}
}
The Composable is called in this way
MyComposable(viewModel = myViewModel, navigateToSecondScreen = {
// action for navigation
})
The apiSuccess
is a LiveData inside ViewModel
whose value is updated after Api Call is success in this way
private val _sendingOtpSuccess: MutableLiveData<Boolean> = MutableLiveData()
val sendingOtpSuccess: LiveData<Boolean> = _sendingOtpSuccess
viewModelScope.launch {
myRepository.makeApiCall(argument = argument).collect { responseState ->
when (responseState) {
is ResponseState.Loading -> {
}
is ResponseState.Success -> {
_sendingOtpSuccess.value = true
}
is ResponseState.Error -> {
}
}
}
}
I got this error
E/AndroidRuntime: FATAL EXCEPTION: main
Process: co.myProject.compose, PID: 10904
java.lang.OutOfMemoryError: Failed to allocate a 10383376 byte allocation with 6291456 free bytes and 8530KB until OOM, target footprint 47887792, growth limit 50331648
With this above code when Api Call is Success the Composable keep continue observing and after some time App gets crashed showing memory issue. What is going wrong here ? Any help will be appreciated
android
android-jetpack-compose
android-livedata
android-architecture-components
stateflow
0 Answers
Your Answer