Home > Mobile >  It is better to call a Room DataBase query many times or create a variable?
It is better to call a Room DataBase query many times or create a variable?

Time:02-02

It is something that may seem very new but I have this doubt, I will do it with a simple example:

I have a database with a table that contains, among many other things, a "name" column

When I want to use the name of id 1 many times, what is the best practice?

Option 1: Create a String variable called "name" to make a single query in the database Option 2: Call "name" directly from the database every time it is to be used

I always create a variable to avoid many queries in my database but I don't know if I'm right

String name = appDataBase.tableDao().getName(id);
    textView1.setText(name);
    textView2.setText("Employee: "   name);
    .
    .
    .

CodePudding user response:

  1. If you are not modifying the value, it is preferred to Create a String variable called "name" to make a single query in the database
  2. If you are modifying it , it is preferred to Call "name" directly from the database every time it is to be used
  3. If somebody else can modify the name, and you need to see the latest value, Get it from the database directly
  •  Tags:  
  • Related