locked
Load data from API once and use it throught multiple pages, where to store List with data RRS feed

  • Question

  • User386791 posted

    When users opens app, FirstPage is loaded, where there is a list of news and some other non-related data is loaded. Then I have a separated page (NewsPage) where same news are loaded (I get data for both pages from one URL). I want to optimize this, so app doesn't go out to get the same data twice, and when news are loaded on FirstPage, I want to store them somewhere and then access it from NewsPage.

    From pageVM I call NewsService, which returns List. Do I set public List here?

    What's the best approach to do this? Where do I store List so both Pages can access, read and modify this list (I'm using MVVM pattern with no frameworks).

    Sunday, October 20, 2019 11:57 AM

Answers

  • User196244 posted

    There is no single (correct) solution. A solution since you are using MVVM and thus have 2 view models, once for FirstPage, another one for NewsPage and since NewsPage must somehow be "loaded" by some code of FirstPage, you could pass the list of news when instantiating the view model of NewsPage, before binding it to NewsPage. Or if NewsPage instantiates its view model on its own, then you could do that as a constructor argument of NewsPage.

    As said, this is just a solution. Another approach is some global storage. Still another one is to make your list public in the view model of FirstPage and access it from NewsPage but this probably means breaking a bit the separation concern of MVVM. But possibly not that much, it really depends on your app and what is actually "FirstPage".

    HTH

    • Marked as answer by Anonymous Thursday, June 3, 2021 12:00 AM
    Sunday, October 20, 2019 2:49 PM
  • User386791 posted

    Since I have static class ApplicationSettings, where I keep some stuff about application. There I created public static List and saved it in there. I hope this doesn't break MVVM too much.

    • Marked as answer by Anonymous Thursday, June 3, 2021 12:00 AM
    Tuesday, October 22, 2019 8:57 AM

All replies

  • User196244 posted

    There is no single (correct) solution. A solution since you are using MVVM and thus have 2 view models, once for FirstPage, another one for NewsPage and since NewsPage must somehow be "loaded" by some code of FirstPage, you could pass the list of news when instantiating the view model of NewsPage, before binding it to NewsPage. Or if NewsPage instantiates its view model on its own, then you could do that as a constructor argument of NewsPage.

    As said, this is just a solution. Another approach is some global storage. Still another one is to make your list public in the view model of FirstPage and access it from NewsPage but this probably means breaking a bit the separation concern of MVVM. But possibly not that much, it really depends on your app and what is actually "FirstPage".

    HTH

    • Marked as answer by Anonymous Thursday, June 3, 2021 12:00 AM
    Sunday, October 20, 2019 2:49 PM
  • User386791 posted

    Since I have static class ApplicationSettings, where I keep some stuff about application. There I created public static List and saved it in there. I hope this doesn't break MVVM too much.

    • Marked as answer by Anonymous Thursday, June 3, 2021 12:00 AM
    Tuesday, October 22, 2019 8:57 AM