gpt4 book ai didi

android - 如何在使用 Room 时立即获取查询结果?

转载 作者:行者123 更新时间:2023-12-03 13:34:04 32 4
gpt4 key购买 nike

我希望一次获得 Room 数据库中所有记录的总数。但是,通常 Room 使用后台线程来异步查询记录。
如果我使用 getTotalOfVoiceAsLiveData()在代码 A 中,它将返回 LiveData<Long>,你知道 LiveData 变量是惰性的,可能结果为空。
如果我使用 getTotalOfVoice()在代码 A 中,我会收到错误,因为我不能使用 returnviewModelScope.launch{ } .
如何使用 Room 数据库一次获取所有记录的总数?
代码 A

class HomeViewModel(val mApplication: Application, private val mDBVoiceRepository: DBVoiceRepository) : AndroidViewModel(mApplication) {

fun getTotalOfVoice():Long {
viewModelScope.launch {
return mDBVoiceRepository.getTotalOfVoice() //It will cause error
}
}

fun getTotalOfVoiceAsLiveData(): LiveData<Long>{
return mDBVoiceRepository.getTotalOfVoiceAsLiveData() //It's lazy, maybe the result is null.
}

}


class DBVoiceRepository private constructor(private val mDBVoiceDao: DBVoiceDao){
suspend fun getTotalOfVoice() = mDBVoiceDao.getTotalOfVoice()

fun getTotalOfVoiceAsLiveData() = mDBVoiceDao.getTotalOfVoiceAsLiveData()
}



@Dao
interface DBVoiceDao{
@Query("SELECT count(id) FROM voice_table")
suspend fun getTotalOfVoice(): Long

//When Room queries return LiveData, the queries are automatically run asynchronously on a background thread.
@Query("SELECT count(id) FROM voice_table")
fun getTotalOfVoiceAsLiveData(): LiveData<Long>
}
添加内容
托比:谢谢!
为什么直接获取数据对您很重要?
我需要根据记录的总数生成一个文件名,例如“untitled0”、“untitled1”、“untitled2”...
如果我可以一次得到查询结果,我可以很容易地使用下面的代码。
再次添加
当我单击开始按钮时,我希望根据查询记录的总数按文件名录制声音。您知道添加或删除记录时,记录总数会发生变化!
代码 B
fun getTotalOfVoice():Long {
//Get the query result at once
...
}

fun createdFileanme(){
return "untitled"+getTotalOfVoice().toString()
}

btnStart.setOnClickListener{
recordVoice(createdFileanme()) //I will record voice by filename
}

fun addRecord(){
...
}

fun deleteRecord(){
...
}
新增内容
谢谢!
我认为 'You should also move all of that into the viewmodel class, without LiveData '是个好办法,可以看图A和 How can I get the value of a LivaData<String> at once in Android Studio? .
你同意吗?
图片A
enter image description here

最佳答案

Question: at once meaning synchronous or what ? if yes, what happens if the function to get the result has to take a longer time? like network call? well you can decide to do that on another thread.


我认为您可以使用可变对象并使用 postValue 函数将结果分派(dispatch)给观察者。它应该如下所示:
class HomeViewModel(val mApplication: Application, private val mDBVoiceRepository: DBVoiceRepository) : AndroidViewModel(mApplication) {
private val voices = MutableLiveData<Long>()

fun getTotalOfVoiceAsLiveData(): LiveData<Long> {
voices.postValue(mDBVoiceRepository.getTotalOfVoiceAsLiveData().value)
return voices;
}
}
在 Fragment 中使用它如下所示:
override fun onActivityCreated(savedInstanceState: Bundle?) {
super.onActivityCreated(savedInstanceState)
if (activity != null) {
val viewModel = ViewModelProvider(requireActivity())
viewModel.get(HomeViewModel::class.java).getTotalOfVoiceAsLiveData().observe(viewLifecycleOwner, Observer { voices: Long ? ->
voices // Sound of music ? be very free to use ...
})
}
}
快乐编码。

关于android - 如何在使用 Room 时立即获取查询结果?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/64478472/

32 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com