1

I am using firestore to save transactions and querying data by timestamp field like that.

CollectionReference IOTransactions = db.collection(userID)
            .document("userData").collection("StockTransactions");

    Query transactionsQuery = IOTransactions
            .whereLessThan("timestamp", dateOfToday())
            .whereGreaterThan("timestamp", findDaysAgo())
            .orderBy("timestamp", Query.Direction.DESCENDING)
            .limit(20);
    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() + " |||| " +queryDocumentSnapshots.);
                        String doc_id = dc.getDocument().getId();
                        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.getOldIndex());
                        adapter.notifyItemRemoved(dc.getOldIndex());
                        break;
                }
            }

My problem is that if a document added to collection, I see in the Firestore console that it counts all documents in the result as "document reads", not only new added document. I checked does it use cache with getMetaData().isFromCache() method and it returns true.

However if I delete where methods and use like that

Query transactionsQuery = IOTransactions
        .orderBy("timestamp", Query.Direction.DESCENDING)
        .limit(20);

then it counts only 1 which is new added document.

I want to query with timestamp but if I can't solve this counting problem I will change my code.

Frank van Puffelen
  • 565,676
  • 79
  • 828
  • 807
ertuzun
  • 153
  • 4
  • 15
  • "it counts all documents as readings not only new added document" As far as I can see your code does not do any counting. Can you explain a bit more what you mean here? Alternatively: what is being logged, and what did you expect to be logged? – Frank van Puffelen Feb 23 '19 at 15:43
  • I am checking counts on usage section of google cloud every time when I add new document. If I use where clause with query it counts as transactionsList.size() however if I delete just where methods then it counts only 1. I know that logs are not count anything in the code they are only for check is data from cache or not. It shows true for isFromCache() method. – ertuzun Feb 23 '19 at 16:02
  • You're only charged for document reads if the document is actually read on the server, so that would mean that the documents are really being read on the server. This should only happen once per 30 minutes for unchanged documents, as the client then rechecks against the server for stale data (as far as I understand it). Also see https://stackoverflow.com/q/53002397, https://stackoverflow.com/q/50782574 – Frank van Puffelen Feb 23 '19 at 16:13
  • Documents say that I will be charged for only query results which is not come from cache and also query will work if user disconnected 30 minutes. However I am seeing 1.5 k document reads with only 30 write operations. To the answer of firestore support while navigating in dashboard of database, documents that are shown also counts as document reads but I didn't navigate on database too much, maybe 3-4 times so it may be counted a hundred from dashboard. But where it come from other 1.4k document reads. It is impossible if it counts only for added item and 30 minutes checks. – ertuzun Feb 23 '19 at 17:45
  • From my experience the Firestore count of document reads has always been correct, so those reads must be coming from somewhere. The console is indeed a common source while you're developing, as are repeated uncached reads, and often also scheduled processes (e.g. cron jobs). But it's impossible for us to know for certain where the reads come from in your case. Consider stopping to use your app and the console for a while, and check if usage still goes up. If it does, there is still another client reading somewhere. – Frank van Puffelen Feb 23 '19 at 18:04
  • I am totally confused. isCacheFrom() returns false when fragment recreated and new document added. I am cleanin cached data or remove app and install again and load fragment again isFromCache returns still false. On the one hand I am checking quota details by waiting 5 minutes and there is no document counts.From my little experience quotas will update in 1 minute but I can't see any changes. Am I using log section wrong way? I didn't understand but sometimes it returns true also; – ertuzun Feb 23 '19 at 19:11

0 Answers0