Skip to content

Instantly share code, notes, and snippets.

@binayshaw7777
Created May 8, 2024 16:05
Show Gist options
  • Select an option

  • Save binayshaw7777/aa943fec06a2ac97964f1dc0126a4d11 to your computer and use it in GitHub Desktop.

Select an option

Save binayshaw7777/aa943fec06a2ac97964f1dc0126a4d11 to your computer and use it in GitHub Desktop.
Android Database builder for Room KMP/CMP
// Save at: composeApp/src/androidMain/kotlin/com/yourOrg/projName/database/Database.kt
import android.content.Context
import androidx.room.Room
import androidx.room.RoomDatabase
import data.database.UserDatabase
fun getUserDatabase(context: Context): RoomDatabase.Builder<UserDatabase> {
val dbFile = context.getDatabasePath("user.db")
return Room.databaseBuilder<UserDatabase>(
context = context.applicationContext,
name = dbFile.absolutePath
)
.fallbackToDestructiveMigrationOnDowngrade(true)
.setDriver(BundledSQLiteDriver()) // Very important
.setQueryCoroutineContext(Dispatchers.IO)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment