如何从Firebase中检索数据 [英] how to retrieve an array of data from firebase

查看:124
本文介绍了如何从Firebase中检索数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经尝试了几种方法,但基本上,我使用Python上传了我的firebase数据库中的一组数据。在Python中,我将所有东西都添加到数组中,并将其上传到数据库。在Swift中,我试图检索这样的数据

  import UIKit 
import Foundation
import Firebase


class CSGOView:UIViewController,UITableViewDelegate,UITableViewDataSource {
$ b var teams:[String] = []
var times:[String] = []




@IBOutlet weak var tableViewTwo:UITableView!
let ref = FIRDatabase.database()。reference(fromURL:把url拿出来,它是数据库的正确的地址im确定)

覆盖func viewDidLoad(){
super.viewDidLoad()

getTeamsAndTimes()
}

func getTeamsAndTimes(){
// let userID = FIRAuth.auth() .currentUser?.uid
ref.child(Teams)。observeSingleEvent(of:.value,with:{(snapshot)in
//获取用户值
let value = snapshot .value as?[String:String] ?? [:]

为团队价值{
print(team.key)
print(team.value)


$ ...
}){(错误)在
print(error.localizedDescription)
}
}


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


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

let cell:UITableViewCell? =无
返回单元格!





$ b $ p

这不是'尽管当视图加载时没有任何东西被打印到控制台。我有方法运行在ViewDidLoad()。

这里是如何在firebase数据库中的数据(我必须去所以我会上传,当我得到)
$ b

团队
0:团队名称
1:teamtwo

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

事实上,我无法上传图片,直到我有10个代表。



https://ibb.co/dSp5ka



上面的链接会告诉你firebase数据库的样子是怎样的

解决方案

以上职位;这是最好的我可以告诉 -
$ b

安全规则

  {
rules:{

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

团队:{

.read:true,//使其可公开访问
.write:true,

$ b}
}
}

代码

  var dict = NSMutableDictionary()//全球声明

$ b FIRDatabase.database()。reference()。child(shows)。observeSingleEvent(of:.value,with:{(Snapshot )在

中每个在Snapshot.children {

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

}

})


Hi there I have tried this in a couple of ways, but basically, I have an array of data in my firebase database that I uploaded using Python. In Python I added everything to an array and uploaded it to the database. In Swift I have tried to retrieve the data like this

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!

    }

}

This doesn't work though when the view loads nothing is printed to the console. I have the method run in the ViewDidLoad().

here is how the data looks like in the firebase database(I have to go So I will upload it when I get back) roughly it is like

Teams 0: "teamname" 1: "teamtwo"

so there is teams then a plus sign and it drops down to show a dictionary with 0 as the key and "teamname" as the value. I don't have the random id before the keys and values if that helps. I used the set method instead of the push method in python. the set method uploads the data without the id.

turns out I can't upload images anyways till I have 10 rep.

https://ibb.co/dSp5ka

the link above takes you to how the firebase database looks like

解决方案

By the information given by you in your above posts; This is the best i can tell -

Security rules

{
"rules": {

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

  "Teams" :{

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


  }
 }
}

Code

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)

        }

    })

这篇关于如何从Firebase中检索数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆