2017-08-01 110 views
-1

如何從firebase獲取子節點? 火力地堡結構是在這裏:從firebase獲取子節點(Swift)

firebase struct

我需要得到一個孩子「照片」爲每一個孩子以上。後來添加到collectionView中的每個孩子「照片」。

我的火力結構:

{ 

"Студии" : { 
"Бабулька" : { 
    "Photo" : { 
    "image" : "https://firebasestorage.googleapis.com/v0/b/test2-29e4b.appspot.com/o/city3.jpg?alt=media&token=12a87c0c-ade1-43f9-b76b-ecdaa86cf185" 
    }, 
    "address" : "Сатурн", 
    "image" : "https://firebasestorage.googleapis.com/v0/b/test2-29e4b.appspot.com/o/city3.jpg?alt=media&token=12a87c0c-ade1-43f9-b76b-ecdaa86cf185", 
    "image1" : "https://firebasestorage.googleapis.com/v0/b/test2-29e4b.appspot.com/o/city3.jpg?alt=media&token=12a87c0c-ade1-43f9-b76b-ecdaa86cf185", 
    "name" : "Карапулька" 
}, 
"Дубаи" : { 
    "Photo" : { 
    "image" : "https://firebasestorage.googleapis.com/v0/b/test2-29e4b.appspot.com/o/city1.jpg?alt=media&token=c442fd8f-4cc4-4e30-8242-975aaf5427dc" 
    }, 
    "address" : "Тутушка", 
    "image" : "https://firebasestorage.googleapis.com/v0/b/test2-29e4b.appspot.com/o/city1.jpg?alt=media&token=c442fd8f-4cc4-4e30-8242-975aaf5427dc", 
    "name" : "Дубаи" 
}, 
"Лондон" : { 
    "Photo" : { 
    "image" : "https://firebasestorage.googleapis.com/v0/b/test2-29e4b.appspot.com/o/oko1.jpg?alt=media&token=fedea2cb-93b1-496c-9392-0b95f4ada7b5" 
    }, 
    "address" : "Бейкер стрит", 
    "image" : "https://firebasestorage.googleapis.com/v0/b/test2-29e4b.appspot.com/o/oko1.jpg?alt=media&token=fedea2cb-93b1-496c-9392-0b95f4ada7b5", 
    "name" : "Лондон" 
    } 
    } 
} 

感謝您的幫助。

+0

https://stackoverflow.com/a/45369544/7715250 –

+0

請發表您的火力地堡結構爲文本,沒有圖像。這樣,如果我們需要在答案中使用結構,我們不必重新鍵入它。您可以將Firebase結構作爲Firebase控制檯中的文本 - >三個點 - >導出JSON – Jay

+0

感謝您的幫助。我添加了問題的firebase結構。 –

回答

0

要獲取所需的數據,請遍歷主父節點的所有子註釋。假設以下結構

posts 
    post_0 
    Photo 
     image: "https://www....." 
    post_1 
    Photo 
     image: "https://www....." 

和代碼來獲取圖像的URL ...

let ref = self.ref.child("posts") 
ref.observeSingleEvent(of: .value, with: { snapshot in 
    for child in snapshot.children { 
     let snap = child as! DataSnapshot 
     let imageSnap = snap.childSnapshot(forPath: "Photo") 
     let dict = imageSnap.value as! [String: Any] 
     let url = dict["image"] as! String 
     print(url) 
    } 
}) 
+0

謝謝,它的工作原理;)也許你可以提示如何加載一組圖像?例如,如果在每個「照片」中,我將具有「圖像1」,「圖像2」,「圖像3」或更多或更少。 –

+0

不要將數據存儲在Firebase陣列中,因爲Firebase陣列是邪惡看到我對[此問題]的答案(https://stackoverflow.com/questions/43179477/firebase-changing-layout-of-child-data-information-in -Android/43191862#43191862)。它們可以作爲key:value對存儲,其中的鍵由childByAutoId生成。所以它會是images/firebase_key_0:image0和/ images/firebase_key_1:image1等 – Jay