I would like to get a count of items within the collection e.g. total number of images saved by users, so that I can display them in the app’s home page. I have found this documentation from Firebase’s website https://firebase.google.com/docs/firestore/query-data/aggregation-queries. However, when I try to implement the code, I been getting a couple of error.
let query = db.collection("cities").whereField("state", isEqualTo: "CA")
let countQuery = query.count
do {
let snapshot = try await countQuery.getAggregation(source: .server)
print(snapshot.count)
} catch {
print(error);
}
Here are the errors I get. I have imported Firebase, FirebaseFirestoreSwift in the file.
I have try googling and searching for how to get data, but I could not find anything close from the internet. More specifically for SwiftI. Hope you guys can help me. Would appreciate your help.
2
Answers
It looks like you need to update your firebase sdk.
The aggregation queries where introduced in version 10.0.0. If you are on an earlier version update your SPM package to the latest and try again.
It looks like you’re using an SDK version where the COUNT functionality isn’t available yet. Count functionality was introduced relatively recently in Swift SDK version 10.0, so be sure to upgrade.