Data Access Object (DAO)
Learn how to handle cache in an application.
Working with Interfaces
DAO is an interface that contains methods for querying the local database for data. Make sure to note the annotations used because they’re required for the methods to function.
These methods are called and implemented from the repository with the @Insert
, @Query
and @Delete
annotations. This simplifies how we write the methods under these annotations to autogenerate the required code in the background. This way, they facilitate and decrease coding time so we can focus on the most important parts of our application.
Creating ArticleDao
We are going to use the @insert
Annotation to insert articles in the articles
table in the database, the onConflict
parameter is set to OnConflictStrategy.REPLACE
, which means that if there is a conflict, the existing row in the table will be replaced with the new data while the getAllArticles()
method is used to retrieve all articles from the local database by annotating it with the @Query
annotation and specify a SELECT
query to retrieve all articles from all rows and columns in the articles table.
The method returns a Flow
of a list of Article
objects which means its an observable and we can observe it for changes and update the UI.
Get hands-on with 1400+ tech skills courses.