For now I use this code in my DAO:
@Dao
interface NotesDao {
@Query("SELECT * FROM Notes")
fun getAllNotes(): List<Notes?>?
@Query("SELECT * FROM Notes WHERE not hidden AND not grouped")
fun getNotes(): List<Notes?>?
@Query("SELECT * FROM Notes WHERE id = :id")
fun getById(id: Long): Notes?
@Insert(onConflict = OnConflictStrategy.REPLACE)
fun insert(notes: Notes?)
@Update
fun update(notes: Notes?)
@Delete
fun delete(notes: Notes?)
}
But in many tutorials I either see Flowable<List<Notes>>
instead of List<Notes>
or LiveData<List<Notes>>
.
Which approach is preferable?
There are two factors you should consider:
Using Kotlin you have two main options (what's better is a matter of taste. In some tutorials you can see also AsyncTask or Thread executors for switching off the main thread):
suspend
) for insert/update/delete, and LiveData or Flow (from coroutines library) for "live" queries, or suspend
- for "one-shot" queries. suspend
lets to switch from the main thread. If you use LiveData/Flow you don't need suspend
, since Room does it itself.See also official documentation for additional hints.