Skip to content

Instantly share code, notes, and snippets.

@AdamMc331
Created September 16, 2017 18:53
Show Gist options
  • Star 20 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save AdamMc331/5d48220875ce6b8bf0a0c0396ec8a0c0 to your computer and use it in GitHub Desktop.
Save AdamMc331/5d48220875ce6b8bf0a0c0396ec8a0c0 to your computer and use it in GitHub Desktop.
Shows how a RoomDatabase.Callback can be used to add database triggers when a database is created.
@Database(...)
abstract class AppDatabase : RoomDatabase() {
abstract fun appDao(): AppDao
companion object {
private var INSTANCE: AppDatabase? = null
private set
fun getInMemoryDatabase(context: Context): CCDatabase {
if (INSTANCE == null) {
INSTANCE = Room.databaseBuilder(context,
AppDatabase::class.java, "app_database.db")
.addCallback(CALLBACK)
.build()
}
return INSTANCE!!
}
private val CALLBACK = object : RoomDatabase.Callback() {
override fun onCreate(db: SupportSQLiteDatabase) {
super.onCreate(db)
db.execSQL("CREATE TRIGGER ...")
}
}
}
}
@blongho
Copy link

blongho commented Aug 15, 2018

Hello AdamMc331, can you provide the java equivalent of your code?
Thanks in advance

@sitaramj
Copy link

sitaramj commented Dec 4, 2018

Here is the Java equivalent code for Room Database callbacks.

  INSTANCE = Room.databaseBuilder(getApplicationContext(), AppDatabase.class, "app-database-db")
                .addCallback(CALLBACK)
                .build();
     
   private RoomDatabase.Callback CALLBACK =  new RoomDatabase.Callback(){
        @Override
        public void onCreate(@NonNull SupportSQLiteDatabase db) {
            super.onCreate(db);
           db.execSQL("CREATE TRIGGER ...")
        }

        @Override
        public void onOpen(@NonNull SupportSQLiteDatabase db) {
            super.onOpen(db);
        }
    };

@carstenhag
Copy link

Thanks a lot @AdamMc331!
The callback has to be saved as a variable, declaring it inside the callback didn't execute it at all for me.

The Kotlin way to handle the INSTANCE singleton would be like this:

fun getDatabase(context: Context): CrosswordDatabase {
    return INSTANCE ?: Room
            .databaseBuilder(context.applicationContext, CrosswordDatabase::class.java, "crossword_database")
            .addCallback(CALLBACK)
            .build()
}

@h65wang
Copy link

h65wang commented Sep 2, 2019

@carstenhag Your code snippet of handling singleton is missing the critical part of assigning the newly created object to the INSTANCE variable. Perhaps you could write .also {INSTANCE = it} at the end.

@sdzshn3
Copy link

sdzshn3 commented Jul 15, 2020

Can I use INSTANCE in callback? Because I want to put some initial data which has to be added only once. If yes, is there any possibility to implement such with Dagger Hilt?

@agam23
Copy link

agam23 commented Oct 7, 2020

if you meant like pre-populating your db, for sure you can do that in here. i am not sure about Dagger Hilt, though.

@WicherW
Copy link

WicherW commented Jul 23, 2022

Thanks a lot! i was looking for that solution :D

private val CALLBACK = object : RoomDatabase.Callback() {
            override fun onCreate(db: SupportSQLiteDatabase) {
                super.onCreate(db)

                db.execSQL("CREATE TRIGGER ...")
            }
        }

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment