如何从Firebase中检索数据 [英] how to retrieve an array of data from firebase
问题描述
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个代表。
上面的链接会告诉你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.
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屋!