作者热门文章
- Java 双重比较
- java - 比较器与 Apache BeanComparator
- Objective-C 完成 block 导致额外的方法调用?
- database - RESTful URI 是否应该公开数据库主键?
最佳答案
首先,对于我的回答,您需要为每个问题赋予如下值:
{
"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
关于ios - swift + 火力地堡 : randomly picking a group of objects using a query to the server,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35524816/
我是一名优秀的程序员,十分优秀!