swift - 如何允许用户只获取他们自己的数据?迅速
问题描述
我已经在我的应用中实现了 Firebase。我可以按下一个按钮并将所需的数据发送到 Firebase。我还设置了一个表格视图,以便将所需的数据添加到表格视图中。但是,当任何用户按下按钮发送数据和更新表时,所有数据都会被拉出,甚至来自其他用户。似乎正在提取整个数据库,而不是特定于用户的数据库。我希望用户能够根据他们的 uid 而不是其他用户的 uid 获得他们输入/保存的干手选项。我认为这与这部分代码有关: let handDryLocations = ["id":key
我对这一切都很陌生,所以非常感谢任何帮助。这是代码:
import UIKit
import Firebase
import FirebaseDatabase
class HandDrySaveViewController: UIViewController, UITableViewDelegate, UITableViewDataSource {
var refHandrylocations: DatabaseReference!
@IBOutlet weak var BestOption: UILabel!
override func viewDidLoad() {
super.viewDidLoad()
NotificationCenter.default.addObserver(self, selector: #selector(didGetNotification4(_:)), name: Notification.Name("text4"), object: nil)
NotificationCenter.default.addObserver(self, selector: #selector(didGetNotification5(_:)), name: Notification.Name("text5"), object: nil)
if FirebaseApp.app() == nil {
FirebaseApp.configure()
}
refHandrylocations = Database.database().reference().child("users");
refHandrylocations.observe(DataEventType.value, with:{(snapshot) in
if snapshot.childrenCount>0{
self.handDryList.removeAll()
for handDryOptions in snapshot.children.allObjects as![DataSnapshot]{
let handDryObject = handDryOptions.value as? [String: AnyObject]
let handDryLocation = handDryObject?["handrylocation"]
let handDryBest = handDryObject?["handdrybest"]
let handDryString = handDryObject?["handdryoption"]
let handDryId = handDryObject?["id"]
let handDry = HandDryModel(id: handDryId as! String?, location: handDryLocation as! String?, best: handDryBest as! String?, options: handDryString as! String?)
self.handDryList.append(handDry)
}
self.handDryTable.reloadData()
}
})
// Do any additional setup after loading the view.
}
@IBAction func BacktoHDCalcButton(_ sender: UIButton) {
let storyboard = UIStoryboard(name: "Main", bundle: nil)
let vc = storyboard.instantiateViewController(identifier: "TowelVDryer")
vc.modalPresentationStyle = .overFullScreen
present(vc, animated: true)
}
func addHandDryLocation(){
let key = refHandrylocations.childByAutoId().key
let handDryLocations = ["id":key,
"handrylocation": HandDryLocation.text! as String,
"handdrybest" : BestOption.text! as String,
"handdryoption" : HandDryOptionsString.text! as String
]
refHandrylocations.child(key!).setValue(handDryLocations)
OptionSavedMessage.text = "Location Saved"
}
@IBOutlet weak var HandDryOptionsString: UILabel!
@IBOutlet weak var HandDryLocation: UITextField!
@IBOutlet weak var OptionSavedMessage: UILabel!
@IBAction func BackButton(_ sender: UIButton) {
}
@objc func didGetNotification4(_ notification: Notification){
let text = notification.object as! String?
HandDryOptionsString.text = text
}
@objc func didGetNotification5(_ notification: Notification){
let text = notification.object as! String?
BestOption.text = text
}
@IBAction func SaveHandDryButton(_ sender: UIButton) {
addHandDryLocation()
}
@IBOutlet weak var handDryTable: UITableView!
var handDryList = [HandDryModel]()
public func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return handDryList.count
}
public func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "cell", for: indexPath) as! HandDrySavedTableViewCell
let handDry: HandDryModel
handDry = handDryList[indexPath.row]
cell.locationOfOption.text = handDry.location
cell.bestOption.text = handDry.best
cell.optionString.text = handDry.options
return cell
}
}
解决方案
这就是我最终做的事情。我敢肯定它对专业人士来说并不完美,但它可以为我完成工作。
guard let user = Auth.auth().currentUser?.uid else { return }
ref!.child("users").child(Auth.auth().currentUser!.uid).child("Quotes").childByAutoId().setValue(quotesLabel.text!)
推荐阅读
- wordpress-theming - 识别wordpress主题代码进行修改
- c++ - 如何将指针的值存储在递归函数中?
- postgresql - postgresql 中不存在列和表之间的关系,即使它确实存在
- firebase - 错误:未为“对象”类型定义运算符“[]”
- javascript - 背景图像在我的样式表中不起作用
- html - 使用 Node.JS 访问 HTML 页面时在 Heroku 中出现错误“错误:ENOENT:没有这样的文件或目录”
- json - 将配置存储为 JSON 文件或数据库
- manim - 如何更改 x(或 y)轴标签的颜色 [manim]
- c++ - 是否可以使用 clang 格式将单行名称空间放在一条线上?
- javascript - Jquery跳到另一个页面并点击按钮事件