Firebase 未检索数据 (iOS)



我第一次在iOS中玩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"
   }
  }
 }
}

我目前有以下代码尝试检索数据,但它呈现不成功。您能提供的任何帮助将不胜感激!

表视图控制器:

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

最新更新