What would be the right mechanism / scope to cope ...
# coroutines
m
What would be the right mechanism / scope to cope with shared state Flow on Android? As an example, think of unidirectional flow ecommerce cart, i.e. I want to have a global instance of the cart and different viewmodels observing the cart state. So I need the cart to be multicasted, to be a hot Flow. I can think of two approaches in general: 1. Use stateIn and attach it to GlobalScope, the cart will be shared through the application's lifetime. This seems to be discouraged, but maybe it's a legitimate use case? (kind of like autoConnect() in rxjava) 2. Somehow manage the clients (viewmodels) observing the cart flow. I.e. when they start consuming, the cart multicasts. When they all finish consuming, the cart is released but will start working again as soon as another viewmodel wants to consume again (kind of like refCount() with some extra initialisation code in rxjava). But I have no idea how to do it and if it's even possible. 3. Some other idea?...