dave08
05/07/2023, 1:07 PMLazyList in Android Compose, and I want to manage the state of each of those items ONLY when it's displayed (I guess with a separate container for each -- there's also a difference in the state logic for different types of items...), how could I achieve this with Orbit?Mikolaj Leszczynski
05/08/2023, 8:31 AMCompose question.
Speaking from experience - you can have a separate view model per each item in the list, keyed by the item ID or something similar using Compose's viewModel function. Compose will keep track of the ViewModels for you.dave08
05/08/2023, 9:07 AMdave08
05/08/2023, 9:09 AMclose() function on the container... or maybe by losing reference to it, it automatically cancels all it's coroutines?Mikolaj Leszczynski
05/08/2023, 9:16 AMrepeatOnSubscriptiondave08
05/08/2023, 9:16 AMMikolaj Leszczynski
05/08/2023, 9:17 AMrepeatOnSubscription ensures that the flow collection will be cancelleddave08
05/08/2023, 9:18 AMitem goes away means loosing it's reference and being GCed, will that still cancel the flows?Mikolaj Leszczynski
05/08/2023, 9:21 AMrepeatOnSubscription to cancel its block, meaning anything you collect there will be cancelledMikolaj Leszczynski
05/08/2023, 9:21 AMViewModelStoreOwner is aliveMikolaj Leszczynski
05/08/2023, 9:23 AMcoroutineScope it's created with.
If you use Android's VM's that is the ViewModel Scope.
If you want custom lifecycle handling, you have two options:
1. Create the containers using a scope different to the ViewModel and manage that yourself
2. Create a custom ViewModelStoreOwner to manage the ViewModel lifecycledave08
05/08/2023, 9:24 AMan Orbit container lives as long as thesounds like a contradiction toit's created with.coroutineScope
1. Coroutines collecting the state and side effects are cancelled when the compose view leaves the hierarchy
2. Having no subscribers triggers?to cancel its block, meaning anything you collect there will be cancelledrepeatOnSubscription
Mikolaj Leszczynski
05/08/2023, 9:28 AMrepeatOnSubscription are cancelled when subscriptions go away
• Everything else continues running until the ViewModel's coroutineScope is cancelled.dave08
05/08/2023, 9:32 AM• Everything else continues running until the ViewModel'sWow, then it could be nice to haveis cancelled.coroutineScope
ContainerHost have a default CoroutineScope generated along with a cancel() or close() if managed by it... but I guess it would get dangerous if one would pass the viewModel's scope and accidentally cancel it...!dave08
05/08/2023, 9:32 AMMikolaj Leszczynski
05/08/2023, 9:33 AMMikolaj Leszczynski
05/08/2023, 9:33 AMsomeScope.container(...)Mikolaj Leszczynski
05/08/2023, 9:34 AMdave08
05/08/2023, 9:36 AMMikolaj Leszczynski
05/08/2023, 9:36 AMEverything else continues running until the ViewModel'sa problem?is cancelled.coroutineScope
Mikolaj Leszczynski
05/08/2023, 9:36 AMrepeatOnSubscriptionMikolaj Leszczynski
05/08/2023, 9:37 AMMikolaj Leszczynski
05/08/2023, 9:37 AMdave08
05/08/2023, 9:38 AMEverything else means here to be able to say if too many of them running would cause a lot of overhead...Mikolaj Leszczynski
05/08/2023, 9:38 AMstate in memoryMikolaj Leszczynski
05/08/2023, 9:39 AMMikolaj Leszczynski
05/08/2023, 9:40 AMdave08
05/08/2023, 9:40 AMdave08
05/08/2023, 9:41 AMMikolaj Leszczynski
05/08/2023, 9:42 AMrepeatOnSubscription to make sure they're cancelled.
If you need to optimise, you can always do it later.Mikolaj Leszczynski
05/08/2023, 9:42 AMdave08
05/08/2023, 9:44 AMjust go with one ViewModel per item and let Compose manage itHow do I share them between the various list/details views? ONE needs to be for a unique item that can have either a summary or a details data class as it's base.
Mikolaj Leszczynski
05/08/2023, 9:46 AMMikolaj Leszczynski
05/08/2023, 9:46 AMMikolaj Leszczynski
05/08/2023, 9:48 AMdave08
05/08/2023, 9:49 AMdave08
05/08/2023, 9:50 AMMikolaj Leszczynski
05/08/2023, 9:51 AMNavBackStackEntry if you're using Compose Navigation
2. Within the ViewModelStoreOwner which would be the activity in a single activity app - or individual fragmentsMikolaj Leszczynski
05/08/2023, 9:51 AMdave08
05/08/2023, 9:59 AMdave08
05/08/2023, 9:59 AMMikolaj Leszczynski
05/08/2023, 9:59 AMthey'll all still be living.Define living 🙂
dave08
05/08/2023, 9:59 AMMikolaj Leszczynski
05/08/2023, 10:00 AMdave08
05/08/2023, 10:01 AMdave08
05/08/2023, 10:04 AMMikolaj Leszczynski
05/08/2023, 10:13 AMViewModelStoreOwner local to the list - but if you want to share them between the list & detail it becomes a complicated problemMikolaj Leszczynski
05/08/2023, 10:13 AMMikolaj Leszczynski
05/08/2023, 10:14 AMdave08
05/08/2023, 10:16 AMdave08
05/08/2023, 10:17 AMMikolaj Leszczynski
05/08/2023, 10:17 AMdave08
05/08/2023, 10:17 AMdave08
05/08/2023, 10:19 AMMikolaj Leszczynski
05/08/2023, 10:21 AMMikolaj Leszczynski
05/08/2023, 10:22 AMdave08
05/08/2023, 10:22 AMMikolaj Leszczynski
05/08/2023, 10:22 AMMikolaj Leszczynski
05/08/2023, 10:23 AMMeaning rewriting another Orbit, just using persistenceSometimes it is what it is. The number of times I had to write and re-write the Orbit library to get it right... 😅
dave08
05/08/2023, 2:29 PM