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?

2 Answers 2

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."

Sign up to request clarification or add additional context in comments.

Comments

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;

4 Comments

Thanks for the notice! I updated my first post. Anyway, I can't see this entering in the callback methods :S
@Genaut but why are you casting it? Does DatabaseSounds class extend RoomDatabase?
Yep, it extends it but Anyway ask for my to cast
@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 example (I'm sure you know, but just in case).

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.