2017-08-16 65 views
0

我正在嘗試從Firebase加載數據,但過去我已經能夠使用下面的代碼,但是我添加了一個額外的子項,結果下面的代碼現在不起作用。我迷路了。任何幫助將不勝感激。不接收來自Firebase的數據?

圖像是一個UIImageView

Here is the image of my firebase

let databaseRef = Database.database().reference() 

     databaseRef.queryOrderedByKey().observe(.childAdded, with: { 
      snapshot in 

      let content2 = (snapshot.value as? NSDictionary)?["imageWebsite"] as? String ?? "" 
      let content3 = (snapshot.value as? NSDictionary)?["infoWebsite"] as? String ?? "" 

      self.imgURL = content2 
      self.infoURL = content3 

     }) 


     print(imgURL) 
     image.setImageFromURl(stringImageUrl: imgURL) 
+0

當你在'with'塊中打印快照時,你會得到什麼? – Callam

+0

你在「應用程序傳輸安全設置」中「允許任意負載」嗎? – Lausbert

+0

我確實已將所有安全傳輸設置設置爲允許 – Yellow

回答

1

通過將setImageFromURL FUNC在它執行的代碼數據庫查詢之前完成查詢確保objects被加載。

let databaseRef = Database.database().reference() 

     databaseRef.queryOrderedByKey().observe(.childAdded, with: { 
      snapshot in 

      let content2 = (snapshot.value as? NSDictionary)?["imageWebsite4"] as? String ?? "" 
      let content3 = (snapshot.value as? NSDictionary)?["infoWebsite4"] as? String ?? "" 

      self.imgURL = content2 
      self.infoURL = content3 
      print(content2) 

      print(self.imgURL) 
      self.image.setImageFromURl(stringImageUrl: self.imgURL) 


     })