In part 4 of the Architecture Components tutorial, we will create our Repository. The Repository ist a simple Java class that abstracts the data layer from the rest of the app and mediates between different data sources, like a web service and a local cache. It hides the different database operations (like SQLite queries) and provides a clean API to the ViewModel.
Since Room doesn't allow database queries on the main thread, we use AsyncTasks to execute them asynchronously.
LiveData is fetched on a worker thread automatically, so we don't have to take care of this.
Also, we will add a RoomDatabase.Callback to our database builder where we populate our database in the onCreate method so we don't start with an empty table. We can also override onOpen if we want to execute code every time our Room database is opened.
Watch the whole playlist:
Example code, dependencies & instructions:
Subscribe to my channel:
Want more Android tutorials? Check my playlist section:
Follow me on social media: