我正在尝试在我的应用程序中使用 SqlDelight 数据库。
在我的 DAO 中,我有一个名为 getRecipeById 的函数来查询数据库并返回域模型流(Recipe)。下面是该函数的实现:(注:RecipeTable 是表的名称,或者我想我应该将其称为 RecipeEntity)
fun getRecipeById(id: Int): Flow<Recipe> {
return recipeQueries.getRecipeById(id)
.asFlow()
.mapToOne()
.map { recipeTable: RecipeTable ->
recipeTableToRecipeMapper(recipeTable = recipeTable)
}
}
Run Code Online (Sandbox Code Playgroud)
然后我的存储库像这样调用这个函数:(注意:recipeLocalDatabase 是我的 DAO)
suspend fun getRecipeById(id: Int): Flow<RecipeDTO> {
val recipeFromDB: Flow<Recipe> = recipeLocalDatabase.getRecipeById(id)
return recipeFromDB.map { recipe: Recipe ->
recipeDTOMapper.mapDomainModelToDTO(recipe)
}
}
Run Code Online (Sandbox Code Playgroud)
然后我的用例层从存储库调用该函数并传递流程:
suspend fun execute(
id: Int
): UseCaseResult<Flow<RecipeDTO>>
{
return try{
UseCaseResult.Success(recipeRepository.getRecipeById(id))
} catch(exception: Exception){
UseCaseResult.Error(exception)
}
}
Run Code Online (Sandbox Code Playgroud)
现在在 ViewModel 中,我正在收集 RecipeDTO 的流程,如下所示:
var recipeForDetailView: MutableState<RecipeDTO> = mutableStateOf( …Run Code Online (Sandbox Code Playgroud) 所以我是 Kotlin Multiplatform Mobile 和一般移动开发的新手。我正在尝试按照KMM 教程中的本教程在我的项目中使用 Ktor。
添加依赖后,如下图build.gradle.kts(commonMain、androidMain、iosMain的依赖):
import org.jetbrains.kotlin.gradle.plugin.mpp.KotlinNativeTarget
plugins {
kotlin("multiplatform")
id("com.android.library")
}
kotlin {
android()
ios {
binaries {
framework {
baseName = "shared"
}
}
}
val ktorVersion = "1.5.2"
sourceSets {
val commonMain by getting {
dependencies {
implementation("io.ktor:ktor-client-core:$ktorVersion")
}
}
val commonTest by getting {
dependencies {
implementation(kotlin("test-common"))
implementation(kotlin("test-annotations-common"))
}
}
val androidMain by getting {
dependencies {
implementation ("io.ktor:ktor-client-android:$ktorVersion")
}
}
val androidTest by getting {
dependencies {
implementation(kotlin("test-junit"))
implementation("junit:junit:4.13")
} …Run Code Online (Sandbox Code Playgroud) kotlin android-gradle-plugin ktor kotlin-multiplatform ktor-client