mercredi 22 juin 2016

setValue method not updating to Firebase database

I'm trying to test saving data to my Firebase Database but I've been having some problems. Here is the code I have for when the user presses the Submit button:

    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)
    }

No matter what I type in, the app performs the segue and does not update in my Firebase database. Also, if I only input data for one of the fields, it still performs the segue and doesn't display the alert. I thought it was a problem with Firebase itself but it's likely a mistake I just can't find. Any help would be greatly appreciated.

Aucun commentaire:

Enregistrer un commentaire