2

First of all this is my second time using Room for Android =D

I realized that if Android Room jetpack works more stable/robust if you follow the "open (db access) close" practice rather than just open and keep the same instance open through all the app.

I know that Android Room's guidelines say the opposite:

You should follow the singleton design pattern when instantiating an AppDatabase object, as each RoomDatabase instance is fairly expensive, and you rarely need access to multiple instances.

Does this paragraph say that you need to keep the connection with the DB alive the most you can? or you need to keep the same reference in memory of the "Database" object and you can actually open close the connection while keeping the same reference to the same object?

If I try to copy the ".db" file to watch the database data into any SQLite viewer if I do not properly close the connection it may not store my info into tables while if I close the moment after I insert data everything works properly.

I am kind of confusing what is the best approach.

Nicolas Jafelle
  • 2,661
  • 2
  • 24
  • 30
  • 1
    "Does this paragraph say that you need to keep the connection with the DB alive the most you can?" -- yes. "If I try to copy the ".db" file" -- that is the source of your difficulty. Use other solutions (e.g., Stetho) for examining your database without making a copy. – CommonsWare May 28 '18 at 20:50
  • Thanks! Started using Stetho and is a great way to debug the app. – Nicolas Jafelle May 30 '18 at 13:26

1 Answers1

0

According to this questions

Android SQLite closed exception

Best place to close database connection

You don't need to close the Room Database. The opening is expensive not the reference.

norbDEV
  • 4,795
  • 2
  • 37
  • 28