java - Which method of document snapshot field access is faster in Firestore?
问题描述
I have successfully created a query to get data from Firestore. I have this data in my db:
- products
- docId
- name: "bacon"
- category: "food"
- price: 44
When I query I get back a QueryDocumentSnapshot and I noticed three ways to get the value of the name
property. Which is faster and more likely to be used?
query.get().addOnCompleteListener(new OnCompleteListener<QuerySnapshot>() {
@Override
public void onComplete(@NonNull Task<QuerySnapshot> task) {
if (task.isSuccessful()) {
for (QueryDocumentSnapshot document : task.getResult()) {
Map<String, Object> map = document.getData();
String name = map.get("name").toString(); //First option
String name = document.getString("name"); //Second option
Product product = document.toObject(Product.class);
String name = product.getName(); //Third option
}
}
}
});
解决方案
String name = document.getString("name"); //Second option
Its the fastest and memory efficient because there is not any extra object creation part.
推荐阅读
- javascript - memb.guild.roles.find 不是函数
- javascript - 指的是一个值,但在这里被用作一个类型
- webgl - 如何为 webgl 模型实现 gouraud 着色
- css - 如何在 typescript 中使用 css 对象自定义 React-Bootstrap 样式
- next.js - 使用样式组件在 LTR 和 RTL 之间切换?
- nginx - nGinx 没有将请求的 URI 传递给 proxy_pass 服务器
- reactjs - React Native:如何在来自两个不同导航的两个不同屏幕之间导航
- r - 如何在 r 中制作分层集群 pheatmap?
- inno-setup - 根据 Inno Setup 的 Finished 页面上的复选框和单选按钮的状态,在脚本常量中为 Run 部分生成应用程序参数
- html - CSS中常用变量吗?