0
我试图测试将数据保存到我的Firebase数据库,但我遇到了一些问题。下面是代码我有当用户按下提交键:setValue方法未更新到Firebase数据库
var ref = FIRDatabase.database().reference()
var user = FIRAuth.auth()?.currentUser
@IBOutlet weak var titleEntry: UITextField!
@IBOutlet weak var townAndZip: UITextField!
@IBOutlet weak var pickPrice: UISegmentedControl!
@IBOutlet weak var pickDuration: UISegmentedControl!
@IBAction func tapBackground(sender: AnyObject) {
view.endEditing(true)
}
@IBAction func postTask(sender: AnyObject) {
if ((titleEntry.text?.isEmpty) != nil && (townAndZip.text?.isEmpty) != nil) {
var price:String = pickPrice.titleForSegmentAtIndex(pickPrice.selectedSegmentIndex)!
var duration:String = pickDuration.titleForSegmentAtIndex(pickDuration.selectedSegmentIndex)!
self.ref.setValue(["title": titleEntry.text!])
self.ref.child("tasks").child(user!.uid).setValue(["price": price])
self.ref.child("tasks").child(user!.uid).setValue(["duration": duration])
self.ref.child("tasks").child(user!.uid).setValue(["town_zip": townAndZip.text!])
self.performSegueWithIdentifier("postAdd", sender: self)
print("Posted!")
} else {
let alert = UIAlertController(title: "Error", message: "Please make sure you filled in everything.", preferredStyle: UIAlertControllerStyle.Alert)
alert.addAction(UIAlertAction(title: "Understood!", style: UIAlertActionStyle.Default, handler: nil))
self.presentViewController(alert, animated: true, completion: nil)
}
不管我怎么输入,应用程序执行SEGUE和我的火力地堡数据库没有更新。另外,如果我只输入其中一个字段的数据,它仍会执行搜索并且不显示警报。我认为这是Firebase本身的问题,但可能是我找不到的错误。任何帮助将不胜感激。
我不确定这是它,但你的if语句是检查是否有任何文本字段是空的吗?那你为什么要检查它!= nil当它已经在isEmpty上返回一个布尔值时?尝试删除该文件 –
它应该是'if!(titleEntry.text?.isEmpty)' –
受过教育的猜测:您的代码是否在用户登录后运行?如果没有,数据库默认配置为拒绝未经身份验证的访问。请参阅此页面上的第一个蓝色注释:https://firebase.google.com/docs/database/ios/save-data –