I’ve been trying to implement a rudimentary follower feed system using firestore in my swift app. I’ve got firestore set up with three top level collections: Users, Reviews, and Comments. I wish to populate my feed with the most recent reviews that a particular set of users posted. To do this, I first need to fetch the set of users from the Users collection (the users I currently follow) and then use documentIDs from these (users I follow) to fetch the respective reviews from the Reviews collection (3 most recent reviews per user I follow).
However, since calls through the SDK are async, I’m struggling to fetch the users first and THEN the reviews for my feed. I’m pretty new to async and await though I’ve gained a somewhat thorough understanding of concurrency in swift. Can someone point me in the right direction?
The code for fetching the users is as follows:
private func fetchReviews(for userID: String) {
let reviewRef = FirebaseManager.shared.firestore.collection("Reviews")
reviewRef
.whereField("uid", isEqualTo: userID)
.order(by: "createdAt", descending: true)
.limit(to: 3)
.getDocuments { querySnapshot, error in
guard let documents = querySnapshot?.documents, error == nil else { return }
reviews = documents.compactMap({ queryDocumentSnapshot in
try? queryDocumentSnapshot.data(as: Review.self)
})
}
}
If I define a function to fetch the users I currently follow:
// Fetches all users I follow and stores it in the usersIFollow var.
private func fetchUsersIFollow() {
// fetch the "following" subcollection within the current user's document.
guard let userID = FirebaseManager.shared.auth.currentUser?.uid else { return }
let db = FirebaseManager.shared.firestore
db.collection("Users").document(userID).collection("Following").getDocuments { querySnapshot, error in
guard let documents = querySnapshot?.documents, error == nil else { return }
usersIFollow = documents.compactMap { queryDocumentSnapshot in
try? queryDocumentSnapshot.data(as: User.self)
}
}
}
(where usersIFollow: [User] is a state variables) and then call it in the fetchReviews() method like so
private func fetchReviews(for userID: String) {
fetchUsersIFollow()
let reviewRef = FirebaseManager.shared.firestore.collection("Reviews")
reviewRef
.whereField("uid", isEqualTo: userID)
.order(by: "createdAt", descending: true)
.limit(to: 3)
.getDocuments { querySnapshot, error in
guard let documents = querySnapshot?.documents, error == nil else { return }
reviews = documents.compactMap({ queryDocumentSnapshot in
try? queryDocumentSnapshot.data(as: Review.self)
})
}
}
it doesn’t work since both are async calls. How do I tweak this?
Note: I know this isn’t probably the best way of handling a feed system. It’s just a basic implementation which I’ll further alter as I develop my app.
2
Answers
There’s two approaches that you can take:
fetchReviews
in thegetDocuments
closure infetchFollowers
.getDocuments
, makefetchFollowers
andfetchReviews
async, and then call them from within aTask
block in the right order. Task is used to call asynchronous code from within synchronous code by running it on a separate thread.Pseudocode for (1) the closure approach:
Pseudocode for (2) the async approach:
I think you can use closures to fetch the users that you follow first, and then use their document IDs to fetch their reviews,
This way, you can fetch the users that you follow first, and then use their document IDs to fetch their reviews.