Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/apache-flex/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Android 如何从房间中获取实时数据?_Android_Kotlin_Android Room_Android Livedata_Android Viewmodel - Fatal编程技术网

Android 如何从房间中获取实时数据?

Android 如何从房间中获取实时数据?,android,kotlin,android-room,android-livedata,android-viewmodel,Android,Kotlin,Android Room,Android Livedata,Android Viewmodel,我正试图从房间里获取实时数据。所以,如果数据库中的任何内容发生更改,我的RecycleView可以进行实时更新 我尝试过使用out LiveData,但当我添加LiveData时,总是会显示此错误 error: Not sure how to convert a Cursor to this method's return type (androidx.lifecycle.LiveData<java.util.List<com.example.models.Club>&

我正试图从房间里获取实时数据。所以,如果数据库中的任何内容发生更改,我的RecycleView可以进行实时更新

我尝试过使用out LiveData,但当我添加LiveData时,总是会显示此错误

error: Not sure how to convert a Cursor to this method's return type     (androidx.lifecycle.LiveData<java.util.List<com.example.models.Club>>).
public abstract java.lang.Object     getAll(@org.jetbrains.annotations.NotNull()
电话号码应该是列表,但我使用TypeConverter将其转换为json和json

TypeConverter在这里

TypeConverter
class ConvertersDB {
    @TypeConverter
    fun fromString(value: String): ArrayList<String> {
        val listType = object : TypeToken<ArrayList<String>>() {

        }.type
        return Gson().fromJson(value, listType)
    }

    @TypeConverter
    fun fromArrayList(list: ArrayList<String>): String {
        val gson = Gson()
        return gson.toJson(list)
    }
}
在我的片段中,我需要通过ViewModel和Repository观察数据库中的所有俱乐部,并将它们发送到RecycleView

现在我得到一个错误:

error: Not sure how to convert a Cursor to this method's return type     (androidx.lifecycle.LiveData<java.util.List<com.example.models.Club>>).
public abstract java.lang.Object     getAll(@org.jetbrains.annotations.NotNull()
顶级梯度

buildscript {

    repositories {
        google()
        jcenter()
    }
    dependencies {
        classpath 'com.android.tools.build:gradle:3.4.1'
        classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:1.3.31"
        classpath "android.arch.navigation:navigation-safe-args-gradle-plugin:1.0.0"


        // NOTE: Do not place your application dependencies here; they belong
        // in the individual module build.gradle files
    }
}

allprojects {
    repositories {
        google()
        jcenter()
    }
}

task clean(type: Delete) {
    delete rootProject.buildDir
}

ext {
    roomVersion = '2.1.0'
    archLifecycleVersion = '2.2.0-alpha01'
    androidxArchVersion = '2.0.0'
    coroutines = '1.2.0'
}

如注释中所述,删除
suspend
。 当一个方法返回一个可观察对象时,没有理由让它挂起,因为它只返回一个对象,并且在观察到它之前不会运行任何查询

@Query("SELECT * FROM club")
fun getAll(): LiveData<List<Club>>
@Query(“从俱乐部中选择*)
fun getAll():LiveData

Room的协程集成提供了返回挂起值的功能,但是当值本身是asnyc时,没有理由使用它。

我发现了一个很好的解决方案,可以将rxJava与Room和livedata一起使用。 该方法类似于使用RxJava进行改造,使用LiveDataReactiveStreams,后者 默认情况下,使用flowable

在你的方法中,我建议:

@Query("SELECT * FROM club")
suspend fun getAll(): LiveData<List<Club>>
@Query(“从俱乐部中选择*)
suspend fun getAll():LiveData
改为

道:

@Query(“从俱乐部中选择*)
fun getAll():(任何rxJava对象)可流动
存储库:

fun getAllClubs() : LiveData<List<Club>>{
   return LiveDataReactiveStreams.fromPublisher(dao.getAll()"do any transformation or 
   other functionality you want and then if using Maybe for example transform to 
   flowable by .toFlowable()")
}
fun getAllClubs():LiveData{
返回LiveDataReactiveStreams.fromPublisher(dao.getAll()“执行任何转换或
您想要的其他功能,然后如果使用,例如转换
可通过.toFlowable()进行流动)
}
视图模型:

val clubList : MediatorLiveData<List<Club>>()

fun setClubData(){
   clubList.addSource(repository.getAllClubs(), {
   clubList.value = it
   })
}
val clubList:MediatorLiveData()
fun setClubData(){
clubList.addSource(repository.getAllClubs(){
clubList.value=it
})
}
然后,您可以像往常一样在视图中观察livedata对象


希望我写的东西是可以理解的,如果不是,我会努力让它更好:)

ClubDao
中,当fun返回类型是
LiveData
时,它不需要是
suspend
fun.@veritas1当使用Coroutines with room时,我不应该使用suspend,这样它就不会在主线程中执行查询了吗?不,当返回类型为
LiveData
@veritas1时,房间将确保在后台线程上为您执行查询。在房间2.1中,支持co例程,您需要暂停;这就是框架了解并提供正确上下文的方式。(来源:)@urukmatra您使用的是什么版本的Room和Kotlin。
@Query("SELECT * FROM club")
fun getAll(): LiveData<List<Club>>
@Query("SELECT * FROM club")
suspend fun getAll(): LiveData<List<Club>>
@Query("SELECT * FROM club")
fun getAll(): (Any rxJava object) Flowable<List<Club>>
fun getAllClubs() : LiveData<List<Club>>{
   return LiveDataReactiveStreams.fromPublisher(dao.getAll()"do any transformation or 
   other functionality you want and then if using Maybe for example transform to 
   flowable by .toFlowable()")
}
val clubList : MediatorLiveData<List<Club>>()

fun setClubData(){
   clubList.addSource(repository.getAllClubs(), {
   clubList.value = it
   })
}