for Android without requiring a context? I want all of my database work to be handled by shared code and I don't love having to instantiate it all by passing in an Android context
m
Matt Nelson
02/11/2022, 4:32 AM
I believe SQLDelight docs explain how to use expect/actual for creating the SQLDriver, which would allow you to then write all of your DB code in commonMain.
You need context for android, but that doesn’t mean you can create your drivers in shared code
Copy code
expect class DatabaseDriver {
suspend fun createDriver(schema: Schema, name: String): SqlDriver
}
and on android
Copy code
actual class DatabaseDriver(val context: Context) {
actual suspend fun createDriver(schema: Schema, name: String): SqlDriver =
AndroidSqliteDriver(schema, context, name)
}
c
clark
02/11/2022, 3:39 PM
The only thing is that at some point, your Android app has to pass the database a context, so then the rest of the shared code has to wait for an external dependency before it can get access to the database which I feel like makes DI more difficult
m
Matt Nelson
02/11/2022, 5:31 PM
Should have
applicationContext
&
Application
available from the get-go via DI to instantiate the Driver...