无法在KMM项目中注入sqldelight

发布于 2025-02-02 11:45:26 字数 2374 浏览 5 评论 0原文

我正在一个小型项目中工作,试图实施KMM。我得到的ISSE与Koin一起,我无法在Android中注入databasedRiverFactory类(基本上我需要上下文)。

这是我的代码:

// 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 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")
    }
}
...

因此,当我在不注入此类的情况下运行项目时,它可以正常工作。我看到了几篇文章,但是在这些帖子中,它们直接在Android/ios中实例化数据库,就我而言,我想将其注入我的sharonemodule中的类中。

我在做什么错?

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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

最美不过初阳 2025-02-09 11:45:26

我发现了什么问题。在我的“ koin.kt”文件中更改了y-&gt;

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

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

需要将“ Platform Module()”方法放在“ CommonModule”的同一级别

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"

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文