当添加新文档时,Firestore addSnapshotListener将查询所有数据-Android

时间:2019-02-22 22:07:06

标签: android google-cloud-firestore

我正在使用Firestore保存对象,但意识到当将新文档添加到集合中时,firestore读取配额会随着查询返回的文档数量而增加。我在用错误的方式进行Firestore阅读吗?有没有更好的方法来减少读数的数量?

我的意思是,有一种类似的方法,如果查询正确,则仅检查新添加的文档,如果是,则将其返回并添加到arraylist。我问这个问题是因为如果一个新文档添加的配额将减少50,如果查询返回50个项目。如果10个新文档添加的配额将增加500。

 Query transactionsQuery = IOTransactions
            .whereLessThan("timestamp", dateOfToday())
            .whereGreaterThan("timestamp", findDaysAgo())
            .orderBy("timestamp", Query.Direction.DESCENDING)
            .limit(40);
    transactionsQuery.addSnapshotListener(new EventListener<QuerySnapshot>() {
        @Override
        public void onEvent(@javax.annotation.Nullable QuerySnapshot queryDocumentSnapshots, @javax.annotation.Nullable FirebaseFirestoreException e) {
            if (e != null) {
                Log.w("FirestoreDemo", "Listen failed.", e);
                return;
            }

            for (DocumentChange dc : queryDocumentSnapshots.getDocumentChanges()) {
                switch (dc.getType()) {
                    case ADDED:
                        Log.d(TAG, "New city: " + dc.getDocument().getData());
                        transactionsList.add(dc.getDocument().toObject(StockTransaction.class));
                        break;
                    case MODIFIED:
                        Log.d(TAG, "Modified city: " + dc.getDocument().getData());
                        break;
                    case REMOVED:
                        Log.d(TAG, "Removed city: " + dc.getDocument().getData());
                        transactionsList.remove(dc.getDocument().toObject(StockTransaction.class));
                        break;
                }
            }

在检查代码和配额时,我意识到,如果我使用whereLessThan和whereGreaterThan方法读取的数值为极限值40。但是,如果我删除where方法,则读取仅计数1。我认为这似乎是一个错误,因为查询只能检查添加的元素。例如,当新文档添加到此集合时,检查它以进行查询,如果查询为true,则返回新添加的文档,如果为false,则不执行任何操作。

0 个答案:

没有答案
相关问题