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

标签 android kotlin android-room android-architecture-components android-livedata

我的 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/

相关文章:

java - Android登录无法连接DB

android - Android停止跨多个 Activity 的SoundPool循环。或在更改页面时停止所有播放声音

android - Kotlin Flow 并行执行两个 API 调用并在每个结果到达时收集它

testing - Camel 3 : How to intercept a route from `onException` using `interceptSendToEndpoint`

java - 如何在房间数据库中创建实体继承?

java - TabBar 应用程序中的 Android MapView -> NullPointerException

安卓模拟器: How to monitor network traffic?

java - 如何将方法调用注入(inject)android中的另一个方法

安卓房间数据库。以表名作为参数创建和删除表

android - 请求ROOM时可以直接使用LiveData吗?