gpt4 book ai didi

ios - Firebase 未检索数据 (iOS)

转载 作者:行者123 更新时间:2023-11-30 12:22:44 25 4
gpt4 key购买 nike

我第一次在 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
}

}

谢谢大家的帮助!

最佳答案

我怀疑您的 uid 返回的可选值导致您无法取回值或数据库规则。尝试以下解决方案,我确保没有选项,并且使用取消 block 来检查对 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")
}

}

关于ios - Firebase 未检索数据 (iOS),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44592891/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com