我已将此添加到我的代码中:
let userID = Auth.auth().currentUser!.uid
ref = Database.database().reference().child(userID);
我认为这会起作用,并且可以为我提供所需的结果,因为我的规则是正确的,但是该应用程序只是提供了一个空白表,而不是在其中填充了详细信息。真的很奇怪!
首先,这是我的Firebase规则:
{
"rules": {
"jobs": {
"$uid": {
".read": "auth.uid === $uid"
}
}
}
}
这是我的Firebase数据库:
这是具有UID的Firebase身份验证
这是我的代码:
用于登录页面的ViewController:
import UIKit
import Firebase
import FirebaseAuth
class LoginViewController: UIViewController {
var ref: DatabaseReference!
static var isAlreadyLaunchedOnce = false
@IBOutlet weak var txtemail: UITextField!
@IBOutlet weak var txtpass: UITextField!
var isSignin:Bool = true
override func viewDidLoad() {
super.viewDidLoad()
if FirebaseApp.app() == nil {
FirebaseApp.configure()
}
}
@IBAction func submit(_ sender: UIButton) {
if let email = txtemail.text, let passowrd = txtpass.text
{ Auth.auth().signIn(withEmail: email, password: passowrd) { (user, error) in
if user != nil {
self.performSegue(withIdentifier: "goto", sender : self)
}
else {
let alert = UIAlertController(title: "Username or Password Incorrect", message: nil, preferredStyle: .alert)
let okButton = UIAlertAction(title: "Ok", style: .default, handler: nil)
alert.addAction(okButton)
self.present(alert, animated: true, completion: nil)
}
}
}
}
}
用于登录页面的视图控制器
import UIKit
import Firebase
class LoggedInViewController: UIViewController, UITableViewDelegate, UITableViewDataSource {
@IBOutlet weak var loginlbl: UILabel!
@IBOutlet weak var tbl: UITableView!
var ref: DatabaseReference!
var jobList = [JobModel]()
func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
let job = jobList[indexPath.row]
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "cell", for: indexPath) as! ViewControllerTableViewCell
let job: JobModel
job = jobList[indexPath.row]
cell.lblCol.text = job.collection
cell.lblDel.text = job.delivery
cell.lblShip.text = job.shipper
cell.lblCon.text = job.consignee
cell.lblEmai.text = job.email
cell.lblRef.text = job.reference
cell.lblFreight.text = job.freight
cell.collected.text = job.collected
cell.delivered.text = job.delievered
return cell }
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return jobList.count
}
override func viewDidLoad() {
super.viewDidLoad()
if FirebaseApp.app() == nil {
FirebaseApp.configure() }
ref = Database.database().reference().child("jobs");
ref.observe(DataEventType.value) { (snapshot) in
if snapshot.childrenCount>0 {
self.jobList.removeAll()
for jobs in snapshot.children.allObjects as! [DataSnapshot]{
let jobObject = jobs.value as? [String: AnyObject]
let jobId = jobObject?["id"]
let jobShipper = jobObject?["shipper"]
let jobConsignee = jobObject?["consignee"]
let jobEmail = jobObject?["email"]
let jobReference = jobObject?["reference"]
let jobFreight = jobObject?["freight"]
let jobCollection = jobObject?["collection date"]
let jobDelivery = jobObject?["delivery date"]
let jobPod = jobObject?["pod"]
let jobCollected = jobObject?["collected"]
let jobDelivered = jobObject?["delivered"]
let job = JobModel(id: jobId as! String?,
shipper: jobShipper as! String?,
consignee: jobConsignee as! String?,
email: jobEmail as! String?,
reference: jobReference as! String?,
freight: jobFreight as! String?,
collection: jobCollection as! String?,
delivery: jobDelivery as! String?,
pod: jobPod as! String?,
collected: jobCollected as! String?,
delivered: jobDelivered as! String?)
self.jobList.append(job)
}
self.tbl.reloadData()
} } }
override func viewDidAppear(_ animated: Bool) {
Auth.auth().currentUser != nil; do {
self.loginlbl.text = "Hello " + (Auth.auth().currentUser?.email)!
}}
}
答案 0 :(得分:0)
您的规则使您可以访问Jobs的子节点。像jobs/user_id_1
因此,您必须听孩子的话,而不是整个jobs
节点
Database.database().reference().child("jobs");
到
Database.database().reference().child("jobs\loggedInUserID");