无法在KMM项目中注入sqldelight
我正在一个小型项目中工作,试图实施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 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我发现了什么问题。在我的“ koin.kt”文件中更改了y-&gt;
我
需要将“ Platform Module()”方法放在“ CommonModule”的同一级别
I found out what was wrong. in my "Koin.kt" file y changed ->
to
I needed to put the "platformModule()" method in the same level at the "commonModule"