2016-09-29 168 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) 

    })