简体   繁体   中英

Unable to inject sqlDelight in Kmm project

I'm working in a small project trying to implement Kmm. The isse I got is with Koin, where I'm not able to inject DatabaseDriverFactory class in Android (where basically I need a Context).

This is my code:

// This code is in "commonMain"
interface MovieDataBase {
    suspend fun deleteAllPreviewMovies()
    suspend fun savePreviewMovies(input: List<PreviewMovieEntity>)
}

class DataBaseImpl(
    // This is the one class im not able to inyect
    databaseDriverFactory: DatabaseDriverFactory
) : MovieDataBase {
    private val database = AppDatabase.invoke(databaseDriverFactory.createDriver())
    private val queries = database.appDataBaseQueries

    override suspend fun deleteAllPreviewMovies() {
        withContext(Dispatchers.Default) { queries.deleteAllPreviewMovie() }
    }

    override suspend fun savePreviewMovies(input: List<PreviewMovieEntity>) {
        withContext(Dispatchers.Default) {
            input.map {
                queries.savePreviewMovies(
                    id = it.id,
                    posterPath = it.posterPath,
                    title = it.title
                )
            }
        }
    }
}

In the other modules:

// in commonMain

expect fun platformModule(): Module
...
val commonModule = module {
    platformModule()
    single<MovieDataBase> { DataBaseImpl(get()) }
    single { provideHttpClient() }
    single<MovieRemote> { MovieRemoteImpl(get()) }
    single<MovieApi> { MovieApiImpl(get(), get()) }
    factory { Dispatchers.Default }
}
...

// in iosMain
actual fun platformModule(): Module = module {
    single { DatabaseDriverFactory() }
    single { MainDispatcher() }
}
...
actual class DatabaseDriverFactory {
    actual fun createDriver(): SqlDriver {
        return NativeSqliteDriver(AppDatabase.Schema, "test.db")
    }
}
...

// in AndroidMain

// here im telling Koin to inyect the Context, but not providing a way (this could be the issue )

actual fun platformModule(): Module = module {
    single { DatabaseDriverFactory(get()) }
    single { MainDispatcher() }
}
...
actual class DatabaseDriverFactory(private val context: Context) {
    actual fun createDriver(): SqlDriver {
        return AndroidSqliteDriver(AppDatabase.Schema, context, "test.db")
    }
}
...

So, when I run the project without injecting this class, it works fine. I saw several post of this, but in those they directly instantiate the dataBase in Android/iOS, in my case I would like to inject this in the classes in my SharedModule .

What am I doing wrong?

I found out what was wrong. in my "Koin.kt" file y changed ->

fun initKoin(appDeclaration: KoinAppDeclaration = {}) = startKoin {
    appDeclaration()
    modules(
        commonModule
    )
}

to

fun initKoin(appDeclaration: KoinAppDeclaration = {}) = startKoin {
    appDeclaration()
    modules(
        platformModule(),
        commonModule
    )
}

I needed to put the "platformModule()" method in the same level at the "commonModule"

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM