When the user presses the findDeal button, the first deal (document) is fetched from a collection named "deals". Once it is fetched, I basically check if that deal (document) already exists in the user's deals history. If it does, then I want to move on to the next deal (document) in the collection and repeat the same process. If it does not exist in the user's deal history, then I proceed to display the deal to the user.
Goal: I do not want to show the user the deals he has already seen so I simply want to ignore a document if it is already seen by the user and proceed to the next one.
I am clueless as to how I can fetch the next document.
Research I have done: I understand that this can be done using pagination but for the life of me can't wrap my head around it. In this use case, there are no buttons or real time listeners that trigger a pagination so how does it work?
fileprivate func findDealPressed(){
Firestore.firestore().collection("deals").order(by: "timestamp").limit(to: 1).getDocuments { (snapshot, error) in
if let error = error{
print(error.localizedDescription)
return
} else {
if let docs = snapshot?.documents, !docs.isEmpty{
for document in docs{
let docValues: [String:Any] = document.data()
let dealId = docValues["dealId"] as! String
let dealAlreadySeenByUser: Bool = self.checkIfDealAlreadySeenByUser(dealId: dealId)
if dealAlreadySeenByUser{
//This is where I want to proceed to the next document in the collection if the value is true i,e if the user has already seen the deal.
} else {
//Proceed to show the deal to the user
self.label = docValues["dealName"] as! String
}
}
}
}
}
}
fileprivate func checkIfDealAlreadySeenByUser(dealId: String) -> Bool{
//This is where I check and confirm it is true or false, whether the user has already seen the deal or not
return true
}