0

Is there a better way to get random data from firestore for both users in the game room simultaneously? What I did is that I created a number array according to the number of questions on the server (therefore, I have to change it every time that I add new question).

func retriveQuestions() {

    let idRef = db.collection("ROOMS_Collection").document(GameData.shared.documentID)
    idRef.getDocument { (docSnapshot, error) in
        guard let docSnapshot = docSnapshot, docSnapshot.exists else {return}
        let data = docSnapshot.data()
        self.questionIDs = (docSnapshot.data()?.compactMap({_ in QuestionID(dictionary: data!)}))!
        let id = self.questionIDs[0]

        let questionRef = self.db.collection("QUESTIONS_Collection").document("\(id.questionID[self.arrayNum])")

        questionRef.getDocument{ (docSnapshot, error) in
            guard let docSnapshot = docSnapshot, docSnapshot.exists else { return }
            let data = docSnapshot.data()
            self.questionArray = (docSnapshot.data()?.compactMap({_ in Question(dictionary: data!)}))!
            let question = self.questionArray[0]

            self.answer = Int(question.answer)!

            self.questionText.fontColor = UIColor.white
            self.questionText.fontName = "Verdana"
            self.questionText.text = ("\(question.question)")
            self.questionText.fontSize = 24.0
            self.questionText.numberOfLines = 0
            self.questionText.lineBreakMode = NSLineBreakMode.byWordWrapping
            self.questionText.zPosition = 5
            self.questionText.position = CGPoint(x: (self.questionBackground?.frame.midX)! - 5, y: (self.questionBackground?.frame.midY)! - 5)
            self.questionText.preferredMaxLayoutWidth = (self.questionBackground?.frame.width)! - 10

            self.addChild(self.questionText)

        }
    }

}
ReyAnthonyRenacia
  • 17,219
  • 5
  • 37
  • 56
Tom B.
  • 124
  • 1
  • 11
  • In your completion closure of `questionRef.getDocument` you have `data` which you then pass in to its own `compactMap`. I don't know what you're trying to do there, but it looks wrong. You're probably creating an array where every question is the same. – Guy Kogus Apr 27 '18 at 09:51
  • During creation of a room, it’s creating 7 random numbers from total numbers of questions. Each number represents document id of questions. After creating the room, it’s fetching numbers from server. By this way, both users answering the same questions. I know it’s wrong. That’s why I am asking better solution for it. – Tom B. Apr 27 '18 at 11:02
  • put firestore data in an array. generate a random number. fetch a random item from the array using the random number. var ranItem = firestoreArray[random] – ReyAnthonyRenacia Apr 27 '18 at 11:36
  • The answer from Dan here lists the best know options: https://stackoverflow.com/questions/46798981/firestore-how-to-get-random-documents-in-a-collection – Frank van Puffelen Apr 27 '18 at 13:49

0 Answers0