问题
I was wondering if someone could either point me to documentation to / clarify when to create or destroy a room database instance. Also how you would effectively open / close it ?
If I have a RoomDatabase object that gets injected via dagger to a presenter is that cool and then I just access various Daos? I'm worried about having the object lingering and taking up memory.
回答1:
Instead of Injecting the RoomDatabase into your presenter you could Inject a DataManager Singleton class.
This DataManager can hold your WeakReferences to your DAOs . Based on when you try to access the data, first check if you have it in memory pass it on to the presenter, if not you can then Lazily access the RoomDB to load data into memory and then pass on to the presenter.
In this way, there is a Single Source of Truth that accesses/manages your data.
来源:https://stackoverflow.com/questions/51031574/when-how-to-create-destroy-room-database