2016-09-29 102 views
0

有什么可以简化这个吗?Firebase查询阵列

databaseRef.child("palettes").queryOrdered(byChild: "0").queryEqual(toValue: text).observeSingleEvent(of: .value, with: { (snapshot) in 

    print (snapshot) 

}) 

databaseRef.child("palettes").queryOrdered(byChild: "1").queryEqual(toValue: text).observeSingleEvent(of: .value, with: { (snapshot) in 

    print (snapshot) 

}) 

databaseRef.child("palettes").queryOrdered(byChild: "2").queryEqual(toValue: text).observeSingleEvent(of: .value, with: { (snapshot) in 

    print (snapshot) 

}) 

databaseRef.child("palettes").queryOrdered(byChild: "3").queryEqual(toValue: text).observeSingleEvent(of: .value, with: { (snapshot) in 

    print (snapshot) 

}) 

databaseRef.child("palettes").queryOrdered(byChild: "4").queryEqual(toValue: text).observeSingleEvent(of: .value, with: { (snapshot) in 

    print (snapshot) 

}) 

我要查询和搜索类似颜色的十六进制代码,然后获得整个数组与它一起,所以我的数据库的安排必须是这样:

enter image description here

+1

昨天你不是问同样的事吗?我在那里告诉你,你需要改变你的数据结构:http://stackoverflow.com/questions/39756884/firebase-query-hex-codes-contained-within-randomly-generated-keys –

+1

对于这个和未来的问题:你在你的问题中包含了JSON树的图片。请用实际的JSON替换为文本,您可以通过点击Firebase数据库控制台中的导出按钮轻松获取。将JSON作为文本可以搜索,使我们能够轻松使用它来测试您的实际数据,并将其用于我们的答案中,并且通常只是一件好事。 –

+0

1)数组是邪恶的,会导致所有问题。请使用childByAutoId并让Firebase为您创建节点密钥。 2)请不要在您的问题中发布图片,只能使用文字。由于结构不得不重新打印,因此图像无法搜索并难以回答。您可以从Firebase控制台导出JSON结构。 3)这是一个重复的问题,因此投票结束。 – Jay

回答

0

尝试这个: -

FIRDatabase.database().reference().child("palletes").queryOrderedByKey().queryStarting(atValue: "0").queryEnding(atValue: "5").observeSingleEvent(of: .value, with: {(snap) in 

    print(snap) 

    })