我尝试测试保存数据到我的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数据库中更新。此外,如果我只输入其中一个字段的数据,它仍会执行segue并且不显示警报。我认为这是Firebase本身的一个问题,但它可能是我无法找到的错误。任何帮助将不胜感激。