简体   繁体   English

多次调用 Room 数据库查询或创建变量更好?

[英]It is better to call a Room DataBase query many times or create a variable?

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?当我想多次使用 id 1 的名称时,最佳做法是什么?

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选项1:创建一个名为“name”的字符串变量以在数据库中进行单个查询选项2:每次使用时直接从数据库中调用“name”

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);
    .
    .
    .
  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如果您不修改值,最好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如果你正在修改它,最好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如果别人可以修改名字,而你需要看最新的值, Get it from the database directly

CRUD operations are always "expensive". CRUD 操作总是“昂贵的”。 Usually they involve network call.通常它们涉及网络调用。 Avoid as.muchbas possible.尽可能避免。 Less is better.越少越好。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM