skip to Main Content

I am trying to paginate the "messages" collection query using limit.

_chatReference
    .orderBy('createdAt', descending: true)
    .limit(limit)
    .snapshots()
    .doOnListen(() => print('Chat stream started with limit $limit'))
    .doOnCancel(() => print('Chat stream cancelled for limit $limit'))

As soon as the user reaches the scroll end I am increasing the limit. But when I increase the limit do I get charged for the last limit + new limit or just the new limit?

Ex- If I call the query with the limit of 20 and then increase it to 40 what is the total read count? Is it 40 or 60?

What if the query stream with a limit of 20 cancelled before listening to the stream with a limit of 40?

2

Answers


  1. If you say that when you first time launch the app, you’re getting 20 documents, then you’ll be charged with 20 document reads. If you increase the limit from 20 to 40, this means that you’ll be charged with 20 more document reads. So there will be a total of 40 reads. You won’t be charged again with the initial 20 reads, because those 20 documents are already in the cache.

    Be aware that this mechanism works, only if you’re attaching a real-time listener. If you intent to use a get() call, you’ll be charged for document reads each time you perform a query.

    Login or Signup to reply.
  2. Firestore Calculation for Paginated Queries

    The Answer

    Let me explain how Firestore manages read counts in your pagination scenario.

    Concerning Active Streams

    _chatReference.orderBy('createdAt', descending: true).limit(limit).snapshots();
    
    

    Changing the barrier from 20 to 40 results in only being charged for 40 reads, not 60. Firestore is clever enough to identify that this is an update to an existing query. The server only sends the required additional 20 documents.

    Regarding Canceled Streams

    If you stop a stream with limit 20 before beginning a new one with limit 40:

    • Initial inquiry: Initial inquiry: 20 exams.
    • The second question had 40 reads
    • Total: 60 readings.

    This is because:

    Cancelling the stream disrupts the connection. The new stream must fetch all documents fresh. Each individual stream counts as a new query.

    Best Practices

    To maximize read counts:

    // Modify the existing stream:
    StreamController<int> limitController = StreamController();
    
    _chatReference
        .orderBy('createdAt', descending: true)
        .limit(limitController.stream.value)
        .snapshots();
    
    // Update the limit
    limitController.add(newLimit);
    

    Instead of adding new streams, use:

    A single stream has a changeable limit. Update the limit as necessary. Keep the stream alive until it is no longer needed.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search