【问题标题】:how to retrieve an array of data from firebase如何从firebase检索数据数组
【发布时间】:2017-03-18 01:26:46
【问题描述】:

您好,我已经尝试了多种方法,但基本上,我的 firebase 数据库中有一组数据,这些数据是使用 Python 上传的。在 Python 中,我将所有内容添加到数组中并将其上传到数据库。在 Swift 中,我试图像这样检索数据

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() 中运行了该方法。

这是firebase数据库中数据的样子(我必须去,所以我回来时会上传它)大致是这样的

团队 0:“团队名称” 1:“团队二”

所以有团队,然后是一个加号,它会下拉以显示一个字典,其中 0 作为键,“团队名称”作为值。如果有帮助,我在键和值之前没有随机 id。我在python中使用了set方法而不是push方法。 set 方法上传不带 id 的数据。

事实证明,在我获得 10 个代表之前,我无论如何都无法上传图片。

https://ibb.co/dSp5ka

上面的链接带您了解 Firebase 数据库的外观

【问题讨论】:

  • 帮不上忙,如果我们看不到数据库结构,请使用 viewDidLoad 函数编辑您的问题。
  • 我更新了代码以显示整个课程我还在底部的那个网站上添加了数据库图片。 @Dravidian
  • 您必须确保您的安全规则允许检索数据
  • 认为这可能是问题所在,我是否必须通过提供用户来做到这一点?或者我可以看到一个如何设置的例子。在另一个类中,我有一个从用户那里获取用户名的类似方法。它是这样的。 ref.child("User").child(userID).我认为用户 ID 是允许我访问数据的原因。但由于我现在提取的数据没有用户 ID 作为数据库中的孩子之一,如图所示。
  • 我无法编辑我的帖子有些我把你的@放在这里@Dravidian

标签: python ios swift firebase firebase-realtime-database


【解决方案1】:

在 JavaScript 中,您可以直接引用 firebaseArray 并从 firebase 获取值作为数组。

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

不确定 Swift/Python。

【讨论】:

  • 我想你是用 javascript 写的,虽然我不确定我可能是错的。你知道 swift 的等价物吗,如果那是 swift,那么我道歉。
  • 对不起,这是javascript。
  • 哦,谢谢你的帮助,如果我用科尔多瓦或其他东西制作这个应用程序,我可能会使用它,但我需要我认为有很大不同的 swift 等价物。
【解决方案2】:

希望对你有帮助

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

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

【讨论】:

  • 这样更好,但它仍然不会向控制台打印任何内容,没有错误。它甚至不会在控制台中产生空格。
  • 所以,经过进一步研究,我发现 for 循环甚至没有执行
  • 如果我在代码将执行的 for 块之后或之前 print('hello') ,但 for 循环内的任何内容都不会执行,至少我不认为@Luan Tran
  • 您好,您可以在 firebase 中查看您的“规则”吗? Rules
【解决方案3】:

根据您在上述帖子中提供的信息;这是我能说的最好的 -

安全规则

{
"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)

        }

    })

【讨论】:

    猜你喜欢
    • 2015-09-07
    • 1970-01-01
    • 2020-12-03
    • 1970-01-01
    • 2016-11-27
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多