我第一次在iOS中使用Firebase。我已经成功地将数据写入firebase并按照这样的方式组织了它:
{
"users" : {
"6ytX7ZLa28MYXX8zm3gawJiOYuY2" : {
"-Kmj196aXH9uRXd-bCgB" : {
"category" : "Work",
"date" : "Jun 16, 1:54 AM ",
"place" : "Tampa, Forida",
"title" : "make an app"
},
"-Kmj1HcNcDb9gD2TTlQB" : {
"category" : "Design",
"date" : "Jun 18, 12:58 AM ",
"place" : "Sarasota",
"title" : "Design an app"
}
}
}
}
我目前有以下代码尝试检索数据,但它渲染不成功。您可以提供的任何帮助将非常感谢!
TableViewController:
import UIKit
import Firebase
import FirebaseDatabase
class TasksHome: UITableViewController {
private var databaseHandle: FIRDatabaseHandle!
var ref: FIRDatabaseReference!
var user: FIRUser!
var username: String!
var items = [Item]()
override func viewDidLoad() {
super.viewDidLoad()
user = FIRAuth.auth()?.currentUser
username = FIRAuth.auth()?.currentUser?.uid
ref = FIRDatabase.database().reference()
startObservingDatabase()
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
}
override func numberOfSections(in tableView: UITableView) -> Int {
return 1
}
override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return items.count
}
override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "Cell", for: indexPath) as! TasksHomeCell
let item = items[indexPath.row]
cell.titleLabel.text = item.title
cell.categoryLabel.text = item.category
cell.placeLabel.text = item.place
cell.dateLabel.text = item.date
return cell
}
override func tableView(_ tableView: UITableView, commit editingStyle: UITableViewCellEditingStyle, forRowAt indexPath: IndexPath) {
if editingStyle == .delete {
let item = items[indexPath.row]
item.ref?.removeValue()
}
}
func startObservingDatabase () {
databaseHandle = ref?.child("users").child(self.username).observe(.value, with: { (snapshot) in
let userDict = snapshot.value as! [String: Any]
let category = userDict["category"] as! String
let date = userDict["date"] as! String
let title = userDict["title"] as! String
let place = userDict["place"] as! String
print("category: \(category) date: \(date) title: \(title) place: \(place)")
})
}
deinit {
ref.child("users/\(self.user.uid)/items").removeObserver(withHandle: databaseHandle)
}
}
项目文件
import Foundation
import FirebaseDatabase
class Item {
var ref: FIRDatabaseReference?
var title: String?
var category: String?
var date: String?
var place: String?
init (snapshot: FIRDataSnapshot) {
ref = snapshot.ref
let data = snapshot.value as! Dictionary<String, String>
title = data["title"]! as String
category = data["category"]! as String
date = data["date"]! as String
place = data["place"]! as String
}
}
谢谢大家的帮助!
答案 0 :(得分:0)
我怀疑你的uid返回的可选值导致你不能获取值或数据库规则。尝试以下解决方案,我确保没有可选项,并且我使用了取消块来检查是否在向Firebase发出查询时出现问题。它是对viewDidLoad的修改,但是不要用它来调用你的startObservingDatabase方法。如果您“找不到用户”,则问题可能出在您的身份验证上。如果你得到“错误观察值...”那么问题可能在于你的数据库安全验证规则,如果它们还没有,请尝试将它们全部公开,看看你是否得到了任何回报。如果你没有得到这些,但现在你用这个解决方案获得了一个值,那么问题可能出在你的firebase引用中的选项。
override func viewDidLoad() {
super.viewDidLoad()
if let user = FIRAuth.auth()?.currentUser {
userId = user.uid
let baseRef = FIRDatabase.database().reference()
databaseHandle = baseRef.child("users").child(userId).observe(.value, with: { (snapshot) in
print("we got a value \(snapshot)")
let userDict = snapshot.value as! [String: Any]
let category = userDict["category"] as! String
let date = userDict["date"] as! String
let title = userDict["title"] as! String
let place = userDict["place"] as! String
print("category: \(category) date: \(date) title: \(title) place: \(place)")
}, withCancel: { (error) in
print("error observing value \(error)")
})
}
else {
print("could not find user")
}
}