Hel*_*oCW 11 kotlin kotlin-coroutines
我正在学习 Kotlin 的协程。
代码 A 来自北极https://github.com/googlecodelabs/android-room-with-a-view
我发现插入和删除功能添加了关键字 suspend。
为什么 Room 中的查询函数不需要getAlphabetizedWords()
在 Kotlin 中添加 suspend 关键字?我认为有些查询函数需要花费很长时间来操作,因此需要在协程中运行。
代码A
@Dao
interface WordDao {
// LiveData is a data holder class that can be observed within a given lifecycle.
// Always holds/caches latest version of data. Notifies its active observers when the
// data has changed. Since we are getting all the contents of the database,
// we are notified whenever any of the database contents have changed.
@Query("SELECT * from word_table ORDER BY word ASC")
fun getAlphabetizedWords(): LiveData<List<Word>>
@Insert(onConflict = OnConflictStrategy.IGNORE)
suspend fun insert(word: Word)
@Query("DELETE FROM word_table")
suspend fun deleteAll()
}
Run Code Online (Sandbox Code Playgroud)
归档时间: |
|
查看次数: |
2818 次 |
最近记录: |