Skip to content

Instantly share code, notes, and snippets.

@binayshaw7777
Last active August 20, 2024 09:01
Show Gist options
  • Select an option

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

Select an option

Save binayshaw7777/1eadc9afaaaa3757c2ef44e0d8d34ca2 to your computer and use it in GitHub Desktop.
iOS Database builder for Room KMP/CMP
// Save at: composeApp/src/iosMain/kotlin/database/Database.kt
import androidx.room.Room
import androidx.room.RoomDatabase
import data.database.UserDatabase
import data.database.instantiateImpl // This shall show error, ignore it
import platform.Foundation.NSHomeDirectory // This shall too in Windows.
fun getUserDatabase(): RoomDatabase.Builder<UserDatabase> {
val dbFile = NSHomeDirectory() + "/user.db"
return Room.databaseBuilder<UserDatabase>(
name = dbFile,
factory = { UserDatabase::class.instantiateImpl() } // This too will show error
)
.fallbackToDestructiveMigrationOnDowngrade(true)
.setDriver(BundledSQLiteDriver()) // Very important
.setQueryCoroutineContext(Dispatchers.IO)
}
@Hanif-MI
Copy link

i am not able to get "import platform.Foundation.NSHomeDirectory " this import

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