So far i've managed to declare expect class for DriverFactory
expect class DriverFactory {
fun createDriver(): SqlDriver
}
actual class DriverFactory(private val context: Context) {
actual fun createDriver(): SqlDriver {
return AndroidSqliteDriver(AppDatabase.Schema, context, "test.db")
}
}
actual class DriverFactory {
actual fun createDriver(): SqlDriver {
return NativeSqliteDriver(AppDatabase.Schema, "test.db")
}
}
Then create the database
class LocalDatabase(databaseDriverFactory: DriverFactory) {
val database = AppDatabase(
driver = databaseDriverFactory.createDriver(),
saved_team_tableAdapter = Saved_team_table.Adapter(
playersAdapter = dbPlayerListAdapter,
playerOffsetsAdapter = dbOffsetListAdapter
)
)
}
But now when i'd like to provide the database, I'm unable to provide the DriverFactory as it's an expect class.
Attempting to provide the specific Android or iOS factory's is also difficult as the Android one needs a context?
val dataModule = DI.Module(name = "dataModule") {
// Need to provide DriverFactory
bindSingleton { LocalDatabase(instance()) }
bindProvider<SavedTeamRepository> { SavedTeamRepositoryImpl(instance()) }
}
The
instance()you are trying to access inbindSingleton { LocalDatabase(instance()) }must be provided somewhere by the KodeIn.instance()value mentioned inbindSingleton { LocalDatabase(instance()) }(this must be under commonMain) would have the provided value by the KodeIn.Now you can provide DriverFactory for iOS as well under iOSMain. This time context is not needed.