Room + ViewModel + LiveData + RecyclerView (MVVM) Part 4 - REPOSITORY - Android Studio Tutorial

Published on Sep 13, 2018 25,836 views

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:





  • Neeraj Bisht
    Neeraj Bisht 6 месяцев назад When will next part come awesome men you are a real inspiration
  • Coding in Flow
    Coding in Flow 6 месяцев назад Soon bro. I also want to start a Retrofit series.
  • downfallparodies1
    downfallparodies1 2 недели назад (изменено) When I run my app after including the call back, I'm getting a database file in my device explorer that is completely empty with no tables or data even though the logcat shows that doInBackground() was called. EDIT: For whatever reason, when I increase the version number of the database, it creates a database with tables but it is not populated.
  • Rambo V
    Rambo V 56 минут назад can we communicate to viewmodel components directly from Room without the use of repository....?
  • Raeid Meer
    Raeid Meer 3 дня назад Bro your tutorials are amazing. Can you please show how we can integrate retrofit in repository ?
  • Cristian Vidal
    Cristian Vidal 3 месяца назад Thank you! very much for this series of videos, I'm returning to android and the design patterns are costing me a lot of effort, this video without DI or RXjava are great because for a newbie like me it's necessary to walk before run.
  • Coding in Flow
    Coding in Flow 3 месяца назад Thank you, this is why I am making these videos! I think it's easier to learn step by step.
  • Sai
    Sai 6 месяцев назад All those individual AsyncTasks can get spaghetti pretty fast, you might wanna consider using Rxjava's Observables to run those Daos in the background instead. Other than that, you covered a pretty tough subject pretty well.
  • Coding in Flow
    Coding in Flow 6 месяцев назад Yea it's true. Also we don't use the dependency injection, which is not optimal. But I try to keep these tutorials modular so they are better understandable. It's a hard task.
  • Breezy Cloud
    Breezy Cloud 6 месяцев назад (изменено) Coding in Flow  Hey, you can see another way to move a business logic from the main thread in the android-sunflower sample. They use `java.util.concurrent.Executors` and it looks neat and easy to understand
  • Coding in Flow
    Coding in Flow 6 месяцев назад Thanks for the tip!
  • Sai
    Sai 6 месяцев назад Yes, Java's ExecutorService is a step in the right direction. Besides AsyncTask android only provide HandlerThread. It is pretty similar to the mentioned ExecutorService in use except HandlerThread is extremely resource efficient. It creates a single background thread where you can loop through an infinite number of runnables. This will increase performance an as a plus clean up the code from messy AsyncTasks.
  • Ralph Manzano
    Ralph Manzano 5 месяцев назад Coding in Flow  optimize this app soon though (make us o rxjava and dagger 2)!! It would really be helpful for us who's trying to learn the industry way of making apps but your tutorial is really helpful!!
  • Sharukh Mohammed
    Sharukh Mohammed 3 месяца назад I am interested in doing this with Kotlin's coroutines, any tut guys?
  • Ibrahim Elmasry
    Ibrahim Elmasry 3 месяца назад Hi I'm new with rxjava , and I don't know what to do , should I use observable or flowable instead of livedata in dao ??
  • Ralph Manzano
    Ralph Manzano 3 месяца назад @Ibrahim Elmasry I use rxjava in dao and in my viewmodel I return livedata, u can achieve it by creating a MutableLiveData in the ViewModel and then when subscribing to Rxjava (in your repository class) set the MutableLiveData's value to the one returned after executing subscribe(), then return the MutableLiveData as LiveData when being called from Fragmen/Activity
  • tej
    tej 6 месяцев назад (изменено) You'v excellently presented the concepts in this series, Please make for MVVM in the continued video.
  • Coding in Flow
    Coding in Flow 6 месяцев назад Thank you!
  • Даулет актобе
    Даулет актобе 6 месяцев назад Finally! I was waiting for this series) Thanks! You are doing really nice job
  • Coding in Flow
    Coding in Flow 6 месяцев назад Thank you! Glad you like it!
  • Sagar Ghare
    Sagar Ghare 3 недели назад Really feeling fun as i am going ahead. thanks
  • Coding in Flow
    Coding in Flow 3 недели назад That's great! Thank you very much!
  • Abdelali Djebara
    Abdelali Djebara 6 месяцев назад Man your awesome i love your work <3
  • Coding in Flow
    Coding in Flow 6 месяцев назад Thank you very much 💚
  • Maxmud Inadullayev
    Maxmud Inadullayev 6 месяцев назад Hi, Thanks a lot for tutorials and I really wait the next parts of tutorials.
  • Coding in Flow
    Coding in Flow 6 месяцев назад The next part will be out tomorrow!
  • Bekircan dal
    Bekircan dal 6 месяцев назад Perfect i can't wait for next episodes 💖
  • Coding in Flow
    Coding in Flow 6 месяцев назад Thank you 💚
  • Tah Che
    Tah Che 6 месяцев назад كنت في إنتظاره بفارغ الصبر ♥
  • Coding in Flow
    Coding in Flow 6 месяцев назад Next part will be out soon!
  • Tah Che
    Tah Che 6 месяцев назад Thank you so much I'm waiting
  • Ibrahim Elmasry
    Ibrahim Elmasry 6 месяцев назад good job keep moving forward (Y)
  • Coding in Flow
    Coding in Flow 6 месяцев назад Thank you! I will!
  • Ehsan Mrf
    Ehsan Mrf 3 месяца назад Excellent _ Thanks so much
  • Coding in Flow
    Coding in Flow 3 месяца назад You're welcome again!
  • Hendrawan Adi
    Hendrawan Adi 1 месяц назад Based on clean architecture, NoteRepository should not know about the outer layer. So instead of passing application parameter, i think we should passing NoteDatabase from ViewModel. CMIIW
  • Tushar Pingale
    Tushar Pingale 6 месяцев назад Please Upload Next Episodes
  • Coding in Flow
    Coding in Flow 6 месяцев назад tomorrow bro
  • Amr mohamed
    Amr mohamed 4 месяца назад thank you for your awesome work
  • Coding in Flow
    Coding in Flow 4 месяца назад You're welcome!
  • Amr Almotalles
    Amr Almotalles 5 месяцев назад First Thank you very much , you are the best. can you explain how to Use Dao inheritance to reduce the amount of boilerplate code of asynctask ?
  • Coding in Flow
    Coding in Flow 5 месяцев назад I am not sure yet what is the best solution here, but I will figure it out soon.
  • Aryan Kumar
    Aryan Kumar 6 дней назад (Void... voids) made me chuckle.
  • Coding in Flow
    Coding in Flow 6 дней назад hehe