Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

When should I build the room database instance?

First time posting a question and I'm a coding noob so sorry if I do something wrong.

I check around and found one answer which is somewhat similar to my question: When / How to Create / Destroy Room Database

However I am not using dagger, nor do I know how to use it.

I wanted to ask when should you build your room database instance? Suppose my app uses the database throughout, should I build it in the OnCreate of the Main Activity? Or should I only build it once the users tries to access some functionality that requires it?

This is the code in question:

instance = Room.databaseBuilder(context.getApplicationContext(), MovieDatabase.class, DB_NAME).build();

So basically when is the best time to run the above code?

Please let me know if this question doesn't make sense.

like image 859
aztek Avatar asked Dec 05 '25 15:12

aztek


1 Answers

Your Database Instance will be built as Singleton and you can access it anywhere as it will be created only once.

Please follow this codelab tutorial to get a clear vision of using Room

like image 147
Mohamed Nageh Avatar answered Dec 07 '25 04:12

Mohamed Nageh



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!