答案 0 :(得分:3)
首先,对于我的回答,你需要给每个问题一个这样的值:
{
"question1": {
"question" : "Do you know swift",
"answer" : "Nope",
"value": 1
},
"question2": {
"question" : "Do you know firebase",
"answer" : "A bit",
"value" : 2
}
}
之后,建议在firebase规则(firebase docs)中添加索引,如下所示:
{
"rules": {
"questions": {
".indexOn": ["value"]
}
}
}
接下来是快速部分:
//Use a for loop to get 10 questions
for _ in 1...10{
//generate a random number between 1 and the amount of questions you have
var randomNumber = Int(arc4random_uniform(amountOfQuestions - 1)) + 1
//The reference to your questions in firebase (this is an example from firebase itself)
let ref = Firebase(url: "https://dinosaur-facts.firebaseio.com/dinosaurs")
//Order the questions on their value and get the one that has the random value
ref.queryOrderedByChild("value").queryEqualToValue(randomNumber)
.observeEventType(.ChildAdded, withBlock: {
snapshot in
//Do something with the question
println(snapshot.key)
})
}
实际的swift代码可能存在缺陷,对于firebase特定代码,请查看Ios documentation