Wednesday, October 21, 2026
HomeiOS Developmentandroid - Unable to inject sqlDelight in Kmm venture

android – Unable to inject sqlDelight in Kmm venture

[ad_1]

im working in a small venture attempting to implement Kmm. The isse i bought is with Koin, the place i´m not in a position to inyect “DatabaseDriverFactory” class in Android ( the place principally i would like a Context ).

That is my code ->

// This code is in "commonMain"
interface MovieDataBase {
    droop enjoyable deleteAllPreviewMovies()
    droop enjoyable savePreviewMovies(enter: Checklist<PreviewMovieEntity>)
}

class DataBaseImpl(
    // That is the one class im not in a position to inyect
    databaseDriverFactory: DatabaseDriverFactory
) : MovieDataBase {
    personal val database = AppDatabase.invoke(databaseDriverFactory.createDriver())
    personal val queries = database.appDataBaseQueries

    override droop enjoyable deleteAllPreviewMovies() {
        withContext(Dispatchers.Default) { queries.deleteAllPreviewMovie() }
    }

    override droop enjoyable savePreviewMovies(enter: Checklist<PreviewMovieEntity>) {
        withContext(Dispatchers.Default) {
            enter.map {
                queries.savePreviewMovies(
                    id = it.id,
                    posterPath = it.posterPath,
                    title = it.title
                )
            }
        }
    }
}

Within the different modules ->

// in commonMain 

anticipate enjoyable platformModule(): Module
...
val commonModule = module {
    platformModule()
    single<MovieDataBase> { DataBaseImpl(get()) }
    single { provideHttpClient() }
    single<MovieRemote> { MovieRemoteImpl(get()) }
    single<MovieApi> { MovieApiImpl(get(), get()) }
    manufacturing facility { Dispatchers.Default }
}
...

// in iosMain
precise enjoyable platformModule(): Module = module {
    single { DatabaseDriverFactory() }
    single { MainDispatcher() }
}
...
precise class DatabaseDriverFactory {
    precise enjoyable createDriver(): SqlDriver {
        return NativeSqliteDriver(AppDatabase.Schema, "take a look at.db")
    }
}
...

// in AndroidMain

// right here im telling Koin to inyect the Context, however not offering a approach (this may very well be the problem )

precise enjoyable platformModule(): Module = module {
    single { DatabaseDriverFactory(get()) }
    single { MainDispatcher() }
}
...
precise class DatabaseDriverFactory(personal val context: Context) {
    precise enjoyable createDriver(): SqlDriver {
        return AndroidSqliteDriver(AppDatabase.Schema, context, "take a look at.db")
    }
}
...

So, when i run the venture with out injecting this class, it really works wonderful. I noticed a number of put up of this, however in these they immediately instantiate the dataBase in Android / ios, in my case i want to inject this within the courses in my SharedModule.

What im doing fallacious ?

Thanks in superior.

[ad_2]

RELATED ARTICLES

LEAVE A REPLY

Please enter your comment!
Please enter your name here

Most Popular

Recent Comments