2017-03-18 42 views
1

您好,我已經嘗試了幾種方法,但基本上,我使用Python上傳的firebase數據庫中有一組數據。在Python中,我將所有內容添加到數組並將其上傳到數據庫。在斯威夫特我試圖找回這樣如何檢索來自Firebase的數據陣列

import UIKit 
import Foundation 
import Firebase 


class CSGOView: UIViewController, UITableViewDelegate, UITableViewDataSource{ 

    var teams: [String] = [] 
    var times: [String] = [] 




    @IBOutlet weak var tableViewTwo: UITableView! 
    let ref = FIRDatabase.database().reference(fromURL: "Took the url out it is the right url to the database im sure") 

    override func viewDidLoad() { 
     super.viewDidLoad() 

     getTeamsAndTimes() 
    } 

    func getTeamsAndTimes() { 
     //let userID = FIRAuth.auth()?.currentUser?.uid 
     ref.child("Teams").observeSingleEvent(of: .value, with: { (snapshot) in 
      // Get user value 
      let value = snapshot.value as? [String: String] ?? [:] 

      for team in value { 
       print(team.key) 
       print(team.value) 
      } 

      // ... 
     }) { (error) in 
      print(error.localizedDescription) 
     } 
    } 


    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int { 
     return 3 
    } 


    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell { 

     let cell: UITableViewCell? = nil 
     return cell! 

    } 

} 

該數據視圖時負載沒有被打印到控制檯不工作,雖然。我有方法在ViewDidLoad()中運行。

這裏的數據看起來像在火力數據庫(我得走了所以我會上傳它,當我回來)大概是喜歡

隊 0:「teamname」 1:「teamtwo的「

所以有團隊然後加號,它下降顯示一個字典與0作爲關鍵和」團隊名稱「作爲價值。如果有幫助,我沒有在鍵和值之前的隨機ID。我使用set方法而不是python中的push方法。 set方法上傳沒有id的數據。

事實證明,我無法上傳圖片,直到我有10個代表。

https://ibb.co/dSp5ka

上面的鏈接將帶您到火力數據庫看起來像

+1

不能真正幫助,如果我們不能看到的數據庫結構,使用* viewDidLoad *函數編輯您的問題。 – Dravidian

+0

我更新了代碼以顯示整個班級,我還將數據庫圖片添加到該網站的底部。 @Dravidian –

+0

您必須確保您的安全規則允許檢索數據 – Dravidian

回答

0

在JavaScript中,你可以直接給firebaseArray參考,並獲得價值從火力點陣列。

var TeamRef = ref.child('Teams'); 
    var TeamInfo = $firebaseArray(TeamRef); 

不確定Swift/Python。

+0

我想你寫在JavaScript中,不知道我可能是錯的。你知道快速的等價物,如果那是迅速的,那麼我道歉。 –

+0

對不起,這是javascript。 –

+0

噢,非常感謝你的幫助,我可能會使用它如果我在科爾多瓦或其他東西做這個應用程序,但我需要我認爲是很多不同的迅速等值。 –

0

希望這可以幫助你

let value = snapshot.value as? [String: String] ?? [:] 

for team of value { 
    print(team.key) 
    print(team.value) 
} 
+0

這樣比較好,但它仍然只是向控制檯打印任何內容,沒有錯誤。它甚至不會在控制檯中留下空白。 –

+0

所以,經過進一步的研究,我已經確定for循環甚至不執行 –

+0

如果我在代碼將執行的for塊之後打印('hello'),但for循環內部的任何內容至少不會執行I不要以爲@Luan Tran –

0

通過在上述職位由您提供的信息;這是最好的,我可以告訴 -

安全規則

{ 
"rules": { 

".write" : "auth != null", 
".read" : "auth != null", 

    "Teams" :{ 

    ".read" : "true", // Make it publicly accessible 
    ".write" : "true", 


    } 
} 
} 

代碼

var dict = NSMutableDictionary() // Global Declaration 


FIRDatabase.database().reference().child("shows").observeSingleEvent(of: .value, with: {(Snapshot) in 

     for each in Snapshot.children{ 

      self.dict.setObject((each as! FIRDataSnapshot).value as! String, forKey: String((each as! FIRDataSnapshot).key) as! NSCopying) 
      print(self.dict) 

     } 

    })