gpt4 book ai didi

android - 实时数据+ View 模型+房间: Exposing a LiveData returned by query which changes over time (Through a fts search)

转载 作者:行者123 更新时间:2023-12-02 16:07:25 25 4
gpt4 key购买 nike

我的 DAO 中有一个 FTS 查询,我想用它在我的应用程序中提供搜索。每次更改搜索文本时, Activity 都会将查询传递给 View 模型。

问题是,Room 每次执行查询时都会返回一个 LiveData ,而我希望在运行查询时更新相同的 LiveData 对象。

我正在考虑将数据从 Room 返回的 LiveData 复制到我的 dataSet (请参阅下面的代码)。这是一个好方法吗? (如果是的话,我实际上该怎么做?)

这是我到目前为止的工作:

在我的 Activity 中:

override fun onCreate(savedInstanceState: Bundle?) {
//....

wordViewModel = ViewModelProviders.of(this).get(WordMinimalViewModel::class.java)

wordViewModel.dataSet.observe(this, Observer {
it?.let {mRecyclerAdapter.setWords(it)}
})
}

/* This is called everytime the text in search box is changed */
override fun onQueryTextChange(query: String?): Boolean {

//Change query on the view model
wordViewModel.searchWord(query)

return true
}

View 模型:


private val repository :WordRepository =
WordRepository(WordDatabase.getInstance(application).wordDao())

//This is observed by MainActivity
val dataSet :LiveData<List<WordMinimal>> = repository.allWordsMinimal


//Called when search query is changed in activity
//This should reflect changes to 'dataSet'
fun searchWord(query :String?) {
if (query == null || query.isEmpty()) {

//Add all known words to dataSet, to make it like it was at the time of initiating this object
//I'm willing to copy repository.allWordsMinimal into dataSet here

} else {

val results = repository.searchWord(query)
//Copy 'results' into dataSet
}
}
}

存储库:


//Queries all words from database
val allWordsMinimal: LiveData<List<WordMinimal>> =
wordDao.getAllWords()

//Queries for word on Room using Fts
fun searchWord(query: String) :LiveData<List<WordMinimal>> =
wordDao.search("*$query*")

//Returns the model for complete word (including the definition for word)
fun getCompleteWordById(id: Int): LiveData<Word> =
wordDao.getWordById(id)
}

DAO:

interface WordDao {

/* Loads all words from the database */
@Query("SELECT rowid, word FROM entriesFts")
fun getAllWords() : LiveData<List<WordMinimal>>

/* FTS search query */
@Query("SELECT rowid, word FROM entriesFts WHERE word MATCH :query")
fun search(query :String) :LiveData<List<WordMinimal>>

/* For definition lookup */
@Query("SELECT * FROM entries WHERE id=:id")
fun getWordById(id :Int) :LiveData<Word>
}

最佳答案

val dataSet :LiveData<List<WordMinimal>>

val searchQuery = MutableLiveData<String>()

init {
dataSet = Transformations.switchMap(searchQuery) { query ->
if (query == null || query.length == 0) {
//return WordRepository.getAllWords()
} else {
//return WordRepository.search(query)
}
}
}


fun setSearchQuery(searchedText: String) {
searchQuery.value = searchedText
}

关于android - 实时数据+ View 模型+房间: Exposing a LiveData returned by query which changes over time (Through a fts search),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58786890/

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