What is the proper way of keeping the state of vm when closing and returning back to the Maui app?
I have a complex form with various input fields such as entries, checkboxes, etc. When the user closes the app and navigates back to the app, I want to ensure that if they return to the form, they can either continue from where they left off or start a new form.
One of my idea is implement Auto-Save in the ViewModel
In the viewmodel implementing an auto save strategy so when any property is changed it saves to cache using Barrel, so it can be loaded from there.
cons this form has few nested collections too means i need to subsctribe to all items and all items's property when added to detect the change, any good advice or libary or tips to make it better?
3 Replies
idk I feel like you can just serialize everything every few seconds
Are you suggesting that I implement a timer to save changes to the cache every 5 seconds without performing change detection?
I’ve already created a test implementation that subscribes to changes for each property, including nested objects and collections. When a property in any nested object changes, an event is fired and propagated up to the root. However, I’ve noticed that when performing operations like adding/removing items in a collection or updating a property multiple times, multiple events are triggered in rapid succession.
To address this and avoid excessive save operations, I introduced a timer with a delay of 3 seconds. If no new triggers occur within that period, the save operation is executed. While this approach technically works, I find it suboptimal and am curious if there are more efficient or elegant solutions to handle this scenario.
I mean, unless there's some global built-in event that triggers on every update automatically, this is optimal
Might be a bit overengineered, but it depends