u/DrawingConsistent729

Sharing complex data across multiple screens

Hello everyone,

Sorry for asking a noob question. I searched a lot about this topic and never found a proper solution.

When sharing complex data across multiple screens, there are a lot of things to consider. During my research, these are the findings I got so far:

Shared ViewModel

It is a good approach, but the problem is that the ViewModel becomes bigger and harder to manage properly. Also, when we need to perform init calls, unnecessary screens trigger the same API because of the shared VM. To avoid that, we can call those init calls in the screen layer, but that comes up with a different issue, which is configuration changes. This re-executes the APIs again.

Singleton Data Layer

A lot of people say, "When you have a strong data layer, there is nothing to worry about." So, in this approach, I use StateFlow to cache the data in the data layer. This approach is really good. In a new VM, I can collect data from it. But there is another issue. For example, there are a few screens: A, B, and C. Screen A is the one that initializes this cached data layer. But when the user leaves screen A and comes back to it, it already has the previous data because of the \@Singleton``. To fix that, I created a function to clear the data layer when the VM onCleared is triggered.

Navigation Arguments

Cannot really rely on that because serializing and deserializing become a mess very quickly.

The complex data I'm talking about is temporary user data, like location, number, favorites, etc. I don't want to save it to the DB until I reach the 4th screen and press Done.

A --> B --> C --> D
|     |      |    |-> Save
|     |      |
|     |      |-> View
|     |
|     |-> Can edit
|
|-> Set user data

Which approach should I take? What are your guys’ suggestions?

reddit.com
u/DrawingConsistent729 — 13 days ago