多次调用 Room DataBase 查询还是创建一个变量更好?
It is better to call a Room DataBase query many times or create a variable?
这看起来很新,但我有这个疑问,我会用一个简单的例子来做:
我有一个 table 的数据库,其中包含一个“名称”列
当我想多次使用 id 1 的名称时,最佳做法是什么?
选项 1:创建一个名为“name”的字符串变量以在数据库中进行单个查询
方案二:每次要使用时直接从数据库中调用“name”
我总是创建一个变量来避免在我的数据库中进行许多查询,但我不知道我是否正确
String name = appDataBase.tableDao().getName(id);
textView1.setText(name);
textView2.setText("Employee: " + name);
.
.
.
- 如果不修改数值,优先
Create a String variable called "name" to make a single query in the database
- 如果是修改,优先
Call "name" directly from the database every time it is to be used
- 如果其他人可以修改名称,而您需要查看最新值,
Get it from the database directly
CRUD 操作总是“昂贵”的。通常它们涉及网络呼叫。避免as.muchbas的可能。越少越好。
这看起来很新,但我有这个疑问,我会用一个简单的例子来做:
我有一个 table 的数据库,其中包含一个“名称”列
当我想多次使用 id 1 的名称时,最佳做法是什么?
选项 1:创建一个名为“name”的字符串变量以在数据库中进行单个查询 方案二:每次要使用时直接从数据库中调用“name”
我总是创建一个变量来避免在我的数据库中进行许多查询,但我不知道我是否正确
String name = appDataBase.tableDao().getName(id);
textView1.setText(name);
textView2.setText("Employee: " + name);
.
.
.
- 如果不修改数值,优先
Create a String variable called "name" to make a single query in the database
- 如果是修改,优先
Call "name" directly from the database every time it is to be used
- 如果其他人可以修改名称,而您需要查看最新值,
Get it from the database directly
CRUD 操作总是“昂贵”的。通常它们涉及网络呼叫。避免as.muchbas的可能。越少越好。