'Efficiency and memory requirements of DataViewModel and Room Database

When implementing a Room ORM for sqlite databases, I am following the practise of making

  1. a singleton AppDatabase class extending RoomDatabase
  2. a Repository class with getters and setters interacting with AppDatabase's Entities and LiveData containing Entities.
  3. a DataViewModel extending AndroidViewModel and providing access to the Repository fields.

Whenever i need asynchronous access to the data, I observe the LiveData that is provided by the Repository. My question is about the memory requirements of DataViewModel.

  1. Should there only be one DataViewModel class in the application?

  2. Should there only be one instance of DataViewModel?

  3. If yes, then what about unnecessary data in the DataViewModel? For example, my constructor looks like:

    public DataViewModel(@NonNull Application application) {
    super(application);
    repository = new Repository(application);
    allCategories = repository.getAllCategories();
    allAvgPoints = repository.getAllAvgRoundPoints();
    avgPoints = repository.getAvgPoints();
    numberOfBronzeMedals = repository.getBronzeMedals();
    numberOfSilverMedals = repository.getSilverMedals();
    numberOfGoldMedals = repository.getGoldMedals();
    

    }

Now assume allCategories represents a large dataset that I seldom need access to. Does it make sense to have this in one single DataViewModel, or does it not affect performance until i actually access the allCategories field?

Right now I have one DataViewModel instance per fragment and I only have one DataViewModel class. Does this result in performance issues or unnecessary data being processed?

TLDR; I don't want to process large datasets unnecessarily. How can DataViewModel be setup to accomplish this?



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source