Nacho Ruiz Martin
03/28/2022, 10:25 AMStore
for convenience.
Normally on Android (I’m not sure on iOS) this `Store`’s lifecycle is bound to the lifecycle of the screen itself (or navigation subgraph) and there’s one store per feature. But on web (if I’m not mistaken) usually there’s a single Store
and it’s bound to the lifecycle of the whole app. This makes for me sense because usually on web there are a lot of components seen at the same time on screen.
My question is, if we’re sharing the Store
logic between mobile and web, does it make sense to have a single Store
or is it better to have multiple Stores
, one per component? Also, does it make sense to bind that to the screen lifecycle on mobile or keep a single instance for the whole app lifecycle?
I know this is completely subjective, I’m only asking for some advice here! And sorry if the terms are not accurate, I’m only experienced on Android and I don’t really know the best practices on web dev and iOS, just what I read online.
Thanks!Lucas Schiavini
03/28/2022, 12:51 PMLucas Schiavini
03/28/2022, 12:51 PMNacho Ruiz Martin
03/28/2022, 1:03 PMCasey Brooks
03/28/2022, 3:25 PMandroidx.lifecycle.ViewModel
to scope the VM to the Fragment, but the pattern works much the same on other targets, too, you just have the manage the lifecycle of the ViewModel on your own.
The Repository layer is what is managing access to APIs, local databases, handling caching logic, etc, and is generally a Singleton. Any data that needs to be shared between multiple screens should be stored in the Repository layer, and each screen looks up the data it needs from the Repository. Each UI should assume that looking up a value by ID could suspend, but the Repository can cache those fetches to provide a better end-user experience. Feel free to break up your Repository layer into smaller classes as fits your app, for example by domain (LoginRepository
, AccountRepository
, BillingRepository
, etc.) or more granular into individual Use-Cases.Nacho Ruiz Martin
03/29/2022, 11:10 AMStore
I was talking about), right? I’m super fine with that; I agree that the ViewModel/Controller shouldn’t really be the one holding the state of the app, but only the state of the view and that is actually nourished from the repository/store layer.
I will give a though on this, then. Maybe the repo layer should hold the app state as a big singleton object a la redux and then expose slice functions for the ViewModel/Controllers to get what the want and to modify that state.
I just also wanted to point you out to this library, I think you can get inspiration from there to your library too.
Thanks again!
Edit: I’ve been digging into your library and I see that you’re also proposing a solution to hold the state in the repository layer, that’s great! I’m getting it correctly by saying that your proposal is to have 1 to 1 screen to ViewModel relationship and N to M ViewModel to Repository relationship? State is really held by the repositories and ViewModel’s own state is getting nourished from there (with any needed transformation).Casey Brooks
03/29/2022, 2:44 PMNacho Ruiz Martin
03/29/2022, 2:58 PM