Mike Simz
Mike Simz

Reputation: 4026

Fetch all documents in collection - Firebase Firestore iOS

I have a "users" collection where each document within the collection represents a single user.

If I fetch all documents within the "users" collection, is that considered 1 read to the firebase usage quota or X reads (X being the number of documents within said collection)?

Current code looks like this:

static func fetchAllUsers(completion: @escaping([TFUser]) -> Void) {
        var users = [TFUser]()
        
        Firestore.firestore().collection("users").getDocuments { (snapshot, error) in
            snapshot?.documents.forEach({ (document) in
                let dictionary = document.data()
                let user = TFUser(dictionary: dictionary)
                users.append(user)
                
                if users.count == snapshot?.documents.count {
                    completion(users)
                }
            })
        }
    }

Any insight on this matter is greatly appreciated. Thanks in advance.

Upvotes: 2

Views: 2500

Answers (1)

cpboyce
cpboyce

Reputation: 207

When querying a collection, you are billed for each document returned from the query.

If possible, instead of pulling in all of the documents and sorting through them to find the ones that you want, you can use the .where() functionality provided by the client SDK.

If you want to save on data usage, since the client SDK does not support partial reads of documents, you can create an HTTPS callable cloud function and only send the desired document data back to the client.

Upvotes: 3

Related Questions