Reputation: 570
I'm seeing such an amount of conflicting advice on this so thought I would ask... How do I load data from an api in a .net maui view using a VM? Here is what I have seen, but none are reliable:
Thanks
Upvotes: 4
Views: 1357
Reputation: 3917
In the VM constructor - only gets called once so not an option
It is not an option, for much more important reasons than the times it is being called.
Use OnNavigatedTo - doesn't get called if the view is in an AppShell tab.
Very good place, however, many times you will not want any updates. And MAUI right now cannot give much info about the navigation event, unless you write some extra code alone.
Use OnNavigatedTo - doesn't get called if the view is in an AppShell tab.
Another very good place, but again, loading data takes time, you need to indicate background work accordingly (Activity indicator, error handling, etc...) Rarely I do API calls here.
I usually handle it by two approaches:
1.Once per page creation (NOT IN CONSTR) and again by user request. (Pull to refresh, etc...)
2.By server message that new data is available.
Upvotes: 1