9
votes

Say I am listening on a document:

db.collection("cities").document("SF")
    .addSnapshotListener { documentSnapshot, error in
      guard let document = documentSnapshot else {
        print("Error fetching document: \(error!)")
        return
      }
      print("Current data: \(document.data())")
    }

Would each time the data is updated count as a read operation, or does the act of listening count itself, according the Firestore billing policy?

Also, say I am listening on a number of documents:

db.collection("cities").whereField("state", isEqualTo: "CA")
    .addSnapshotListener { querySnapshot, error in
        guard let documents = querySnapshot?.documents else {
            print("Error fetching documents: \(error!)")
            return
        }
        let cities = documents.map { $0["name"]! }
        print("Current cities in CA: \(cities)")
    }

Will I be charged for a read operation immediately on all documents that match the query or on each update to each document - or both?

1

1 Answers

13
votes

The act of listening does not itself count as a read, however there is a minimum of one document charged per query. From the pricing page, under "Minimum charge for queries":

There is a minimum charge of one document read for each query that you perform, even if the query returns no results.

The initial read and the updates both count. However if you re-listen a short while after you've already done so, you won't get charged for documents that haven't changed since you listened last. Currently that short while threshold is 30 minutes. That's also on there, under "Listening to query results":

When you listen to the results of a query, you are charged for a read each time a document in the result set is added or updated

Also, if the listener is disconnected for more than 30 minutes (for example, if the user goes offline), you will be charged for reads as if you had issued a brand-new query.