1

I have a litte database with Android Room. I want prepopulate the tables with some data but I can't get trigger the callback with onCreate or onOpen methos.

I have the Database with singleton pattern like this:

    public class DatabaseSoundsInitializer{
    ...
    private static DatabaseSounds databaseObject;
    public static DatabaseSounds getDatabaseSoundsInitializer(Context context, Class klass, String name){
     if(databaseObject == null){
         Log.d(TAG, "Debug: Creating the database");
         databaseObject = (DatabaseSounds) Room.databaseBuilder(context, klass, name).addCallback(rdc).build();
     }
     return databaseObject;
    }

static RoomDatabase.Callback rdc = new RoomDatabase.Callback() {
        @Override
        public void onCreate(@NonNull SupportSQLiteDatabase db) {
            super.onCreate(db);
            Log.d(TAG, "Debug: onCreate");
        }

        @Override
        public void onOpen(@NonNull SupportSQLiteDatabase db) {
            super.onOpen(db);
            Log.d(TAG, "Debug: onOpen");
        }
    };
...
}

I call this from my main Activity like this:

DatabaseSounds db = DatabaseSoundsInitializer.getDatabaseSoundsInitializer(getApplicationContext(), DatabaseSounds.class, Utils.DATABASE_NAME);

Here my database Class:

public abstract class DatabaseSounds extends RoomDatabase{}

I can see the log with Debug: Creating the database but not others. I am making this wrong?

Genaut
  • 1,810
  • 2
  • 29
  • 60

2 Answers2

10

I had a similar problem. The onCreare() didn't get called and in turn nor would the onOpen(). This was because I was never actually using the instance, therefore the database was not being created.

https://stackoverflow.com/a/47619844/3678942

"...until you perform some concrete operation, such as invoking a @Dao method that hits the database, your database will not be created."

bluewhale
  • 181
  • 2
  • 9
2

I believe your getDatabaseSoundsInitializer returns null. Assuming that databaseObject is an instance of a class that extends RoomDatabase it should be:

if(databaseObject == null){
     Log.d(TAG, "Debug: Creating the database");
     databaseObject = Room.databaseBuilder(context, klass, name).addCallback(rdc).build();
 }
 return databaseObject;
Suleyman
  • 2,765
  • 2
  • 18
  • 31
  • Thanks for the notice! I updated my first post. Anyway, I can't see this entering in the callback methods :S – Genaut May 13 '18 at 15:25
  • @Genaut but why are you casting it? Does `DatabaseSounds` class extend `RoomDatabase`? – Suleyman May 13 '18 at 15:32
  • Yep, it extends it but Anyway ask for my to cast – Genaut May 13 '18 at 15:55
  • @Genaut I tested the callbacks in my app and it works. It may probably have to do with your choice of putting `getDatabaseSoundsInitializer ` in `DatabaseSoundsInitializer ` and not in `DatabaseSounds`. Try putting your code directly into the `DatabaseSounds` like in [this](https://codelabs.developers.google.com/codelabs/android-room-with-a-view/#6) example (I'm sure you know, but just in case). – Suleyman May 13 '18 at 16:34