1

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
    }
Archid
  • 377
  • 6
  • 21
  • where do you store if the deal is already seen by the user? – Keshu R. Jan 13 '20 at 07:44
  • @iOSArchitect.com Thanks for the comment. I have a collection "users" that in turn has another subcollection "deals" where all the deals already seen by the user is listed. Did not include that code as I felt it is pretty self explanatory. – Archid Jan 13 '20 at 07:47
  • You're going to have a hard time with this type of query at scale, without a ton of reads, as there are no "NOT IN" (nor join) type queries in Firestore like you'd expect in SQL. https://stackoverflow.com/questions/52085868/firestore-get-documents-where-value-not-in-array – Doug Stevenson Jan 13 '20 at 08:05
  • @DougStevenson Hi Doug. I in fact checked your answer before I posted this question. I know that there is only an array contains option but no NOT option. As for tons of reads, that shouldn't be a problem in my case because very few percentage of deals end up re occurring but still it is a necessary step that I have to take. In you answer, even though it makes sense, how will it work if I do not even know which document to set to TRUE or FALSE. If it is a property day like "gender" your solution works but in this case it is "Deal ID" and there is no way we would know that before hand – Archid Jan 13 '20 at 08:21

0 Answers0