首页 > 解决方案 > 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
            }
        }
    }
});

标签: javaandroidfirebasegoogle-cloud-firestore

解决方案


 String name = document.getString("name"); //Second option 

Its the fastest and memory efficient because there is not any extra object creation part.


推荐阅读