Thread
#compose
    Archie

    Archie

    2 years ago
    I was also wondering if it is possible to run
    Coroutines
    with compose?
    Adam Powell

    Adam Powell

    2 years ago
    launchInComposition
    will launch a coroutine into a scope managed by the composition. If the call leaves the composition the scope is cancelled for you. https://developer.android.com/reference/kotlin/androidx/compose/package-summary#launchincomposition
    Archie

    Archie

    2 years ago
    Wow thank you very much. I've been searching for quite a while but I didnt see this. Thanks!
    So ive tried out
    launchInComposition
    , now I was wondering how I could run my coroutines without it getting cancelled by device rotation similar to launching coroutines in
    viewModelScope
    . Is this possible?
    Adam Powell

    Adam Powell

    2 years ago
    Yes, you can still launch into a
    viewModelScope
    in the usual way
    Archie

    Archie

    2 years ago
    That means I have to create a ViewModel luanch the coroutine inside that ViewModel with viewModelScope? correct?
    Adam Powell

    Adam Powell

    2 years ago
    For the moment anyway. I think we had a couple of compose extensions that behave similarly to the
    by viewModels()
    API for fragments/activities floating around but I'm not sure which things made it into the actual api just yet 🙂
    Timo Drick

    Timo Drick

    2 years ago
    You could also just create a singleton (object) and launch your coroutine there. When you do not want to cancel it when the view gets disposed. But than you can not change the UI as an result of the execution. So it depends on what you want to do.
    Adam Powell

    Adam Powell

    2 years ago
    This is where a pattern of
    launchInComposition
    making suspending calls to a ViewModel makes a lot of sense
    the suspend function on the viewmodel can do things like
    viewModelScope.async { ... }
    internally, store the
    Deferred
    , and return the
    .await()
    of that operation
    then future calls can await the one that's already in progress
    UI updates stay scoped to the UI, and the operation itself stays scoped elsewhere and can multiplex for multiple awaiters
    Timo Drick

    Timo Drick

    2 years ago
    In my opinion in practice often the viewmodel lifecycle is not appropriate for most operations that are not directly tied to a fragment or an activity. But yes of course now with compose we only need one activity. But than the viewmodel is at the end the same like an object?
    Adam Powell

    Adam Powell

    2 years ago
    Substitute your backing repository scope then; the key part is that you let the operation and awaiting its result happen in different scopes 🙂
    tbh I find very little use for
    viewModelScope
    in my own code thanks to this 🤷‍♂️
    Archie

    Archie

    2 years ago
    tbh I find very little use for 
    viewModelScope
     in my own code thanks to this
    I got confused with this. So do we need
    viewModel
    or not? 🤔
    Adam Powell

    Adam Powell

    2 years ago
    ViewModel itself as a transport for transformed data caches across configuration changes, sure, but the
    viewModelScope
    CoroutineScope I don't tend to use very often. It depends on what you want to scope and why
    Archie

    Archie

    2 years ago
    For the moment anyway. I think we had a couple of compose extensions that behave similarly to the 
    by viewModels()
     API for fragments/activities floating around but I'm not sure which things made it into the actual api just yet 🙂
    I accidentally found this in compose:
    val mainViewModel: MainViewModel = viewModel()
    where
    viewModel()
    is a
    @Composable
    function. (Thank for pointing out the existing api ❤️) Is it right to assume that the
    ViewModel
    created here is scope to the
    Activity
    /Fragment Hosting the
    @Composable
    function?