https://kotlinlang.org logo
#compose
Title
# compose
v

Vivek Modi

07/19/2022, 4:34 PM
Hey guys, I am using viewmodel in my compose application. I am calling api in my viewModel through
viewModelScope.launch
so when I call in my compose function, should I use any thing else? I am reading Side-effects using LunchEffect and DispoableEffect. Is this neccessary in my composable function? If yes can someone guide me, bcoz I unable to understand this concept. Thanks
Copy code
class MainActivityViewModel(private val resultRepository: ResultRepository) : ViewModel() {

    init {
        getSportResult()
    }

    fun getSportResult() {
        viewModelScope.launch {
            val response = resultRepository.getSportResult()
            
        }
    }
}
Copy code
@OptIn(ExperimentalMaterial3Api::class)
@Composable
fun SetupView(viewModel :MainActivityViewModel = koinViewModel()) {
    Scaffold(topBar = {
        TopAppBar(
            title = { Text(text = stringResource(id = R.string.app_name)) },
        )
    }, content = { padding ->
        Column(
            modifier = Modifier
                .fillMaxSize()
        ) {
            Button(onClick = { /*TODO*/ }) {
                Text(text = stringResource(id = R.string.get_result))
            }
        }
    })
}
Thanks guys..
z

Zach Klippenstein (he/him) [MOD]

07/19/2022, 5:24 PM
Calling launch from an event handler is fine.
v

Vivek Modi

07/19/2022, 10:13 PM
Ok that's great
5 Views