如何从firestore获取随机数据?

时间:2018-04-27 09:32:45

标签: ios swift firebase google-cloud-firestore

是否有更好的方法可以同时从游戏室中为两个用户从firestore获取随机数据? 我所做的是根据服务器上的问题数量创建了一个数字数组(因此,每次添加新问题时我都要更改它)。

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)

        }
    }

}

0 个答案:

没有答案