CodeNewbie Community 🌱

Cover image for Room database. Repository
Tristan
Tristan

Posted on

Room database. Repository

Introduction

  • This series is going to be dedicated to the basics of an SQLite database. I will be following the official google guide, HERE but I will be working in an order that makes more sense to me.

What is a repository?

  • A repository is not actually part of the Room library. It is just considered a best practice in coding when dealing with databases.
  • Creating a repository class provides us with a clean API for data access to the rest of the application.

Why use a Repository?

  • A repository manages queries and allows us to use multiple backends. A repository implements the logic for deciding whether to fetch data from a network of to use results cached in the local database.

  • Now that we have a basic understanding lets jump into the code and explain things line by line.

public class WordRepository {

    private WordDAO mWordDao;
    private LiveData<List<Word>> mAllWords;

    WordRepository(Application application){
        WordRoomDatabase db = WordRoomDatabase.getDatabase(application);
        mWordDao = db.WordDAO();
        mAllWords = mWordDao.getAllWords();
    }

    LiveData<List<Word>> getAllWords(){
        return mAllWords;
    }

    void insert(Word word){
        WordRoomDatabase.databaseWriteExecutor.execute(()->{
            mWordDao.insert(word);
        });
    }
}

Enter fullscreen mode Exit fullscreen mode

public class WordRepository

  • Just a standard class declaration, nothing fancy here.

private WordDAO mWordDao;

  • This is use creating a private variable of type WordDAO. If you remember, our WordDAO is a data access object for our word entities. This is the class that we use to interact with the underlying SQLite database.

private LiveData< List< Word > > mAllWords;

  • This is a private variable that has a return type of LiveData. What is LiveData? Well, the LiveData class is actually pretty complex and I would recommend that you read more about it HERE. Essentially it employs the observer pattern which means we don't need to update the UI every time the app data changes because the observer does it for us. It also handles all the thread management for us.
  • The angle brackets <<List<Word>> are just java generics that mean LiveData will contain a List that contains Word entities.

WordRepository(Application application){...}

  • This is the constructor for our class and it takes a single argument of type Application, which is our global application state

WordRoomDatabase db = WordRoomDatabase.getDatabase(application)

  • This is how we create a single instance of our database. getDatabase(application) is a static method inside of our WordRoomDatabase class and this method will use application to return an instance of our database. Since WordRoomDatabase employs the singleton pattern we can never have more than one instance of our database.

mWordDao = db.WordDAO();

  • This is just assigning an instance of our WordDAO to the member variable mWordDAO

mAllWords = mWordDao.getAllWords();

  • We access the WordDAO through mWordDao and call getAllWords(), which will return a LiveData class of all the Word entities in our database.

LiveData< List< Word > > getAllWords(){...}

  • This is just a simple getter method for our mAllWords variable.

void insert(Word word){...}

  • This is a void method that we are going to use to insert words into our database. Now if you remember when dealing with our database we do not want to perform transactions on the main thread. So that means we will be using our fixed thread pool. databaseWriteExecutor is a static variable on our WordRoomDatabase. It contains our fixed thread pool that has 4 worker threads that are ready to do asynchronous tasks for us. Then we call execute() which is a void method that takes in a command and will run that command at sometime in the future using our fixed thread pool's worker threads.

  • The "command" that we give the execute method is to insert a word entity inside of our Database. If the syntax is confusing to you, it is because the execute() method is given a lambda expression.

What is a lambda expression?

  • Lambda expressions are a special kind of syntax that allows us to pass functionality as an argument to another method. Similar to a callback methods in JavaScript.

  • If you are wondering why do we have to use a lambda expression and why can we just use mWordDao.insert(word); by itself. It is actually because .execute() only accepts arguments of type Runnable, which is an interface. The reason that we can use lambda expressions is because Runnable is a functional interface, meaning that we can use lambda expressions in its place.

Conclusion

  • Thank you for taking the time out of your day to read this blog post of mine. If you have any questions or concerns please comment below or reach out to me on Twitter.

Top comments (0)