can I pass viewmodel scope in another class? coroutines

Viewed 782

I use suspend function in Pagging Library Jetpack.

in loadAfter() and loadInitial() I use retrofit suspending functions. For this I need to use GlobalScope which is not recomended, How I can pass viewModel's scope?

1 Answers

My solution is usually that I make functions that call suspending functions also suspending and only do calls like viewModelScope.launch(Dispatchers.IO) in the ViewModel that asks for the data. This way the suspend keyword spreads a bit through the application, but I also have the control of stopping such background operations on the UI level, where I can cancel the request if the data is no longer required because the user switched the view.

Related