1

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.

Brenden Belluardo
  • 1,163
  • 1
  • 8
  • 10
  • I'm not sure this is it, but your if statement is checking if any of the text fields are empty right? So why are you checking that it != nil when it is already returning a boolean on isEmpty? Try removing that – nilay neeranjun Jun 21 '16 at 03:24
  • It should be `if !(titleEntry.text?.isEmpty)` – nilay neeranjun Jun 21 '16 at 03:27
  • Educated guess: is your code running after the user has signed in? If not, the database is by default configured to reject unauthenticated access. See the first blue note on this page: https://firebase.google.com/docs/database/ios/save-data – Frank van Puffelen Jun 21 '16 at 03:48
  • If that is indeed the cause, see http://stackoverflow.com/questions/37403747/firebase-permission-denied/37404116#37404116 – Frank van Puffelen Jun 21 '16 at 03:54
  • @nilayneeranjun Thanks! That allowed the error to show up for not filling in all fields, but the database is still not updating. – Brenden Belluardo Jun 21 '16 at 12:34
  • @FrankvanPuffelen I set the rules to allow public read and write capabilities but it is still not updating, which is weird because it appears that the if statement is performing as expected. – Brenden Belluardo Jun 21 '16 at 12:34
  • Next step would be to [add a completion handler to your `setValue()` calls](https://firebase.google.com/docs/database/ios/save-data#receive_a_completion_callback), which will show the reason the write failed. If you have any custom security/validation rules, show them. And realize the each `setValue()` is executed as a separate write operation. – Frank van Puffelen Jun 21 '16 at 14:52
  • @FrankvanPuffelen interesting, I added one like so: `self.ref.setValue((["title": titleEntry.text!]), withCompletionBlock: { (error, FIRDatabaseReference) in print (error?.localizedDescription) })` and no error came up at all! – Brenden Belluardo Jun 21 '16 at 15:08
  • 1
    Did the completion handler fire at all? Note that Stack Overflow is a really inefficient debugging mechanism. To make it slightly better, please make the most out of each interaction. – Frank van Puffelen Jun 21 '16 at 15:19
  • @FrankvanPuffelen Sorry, I am new to iOS programming/debugging so I apologize if I am not doing this properly. I'm not entirely sure how to manage the completion handler so as far as I know, all it did was say print "posted" and return to the home screen. – Brenden Belluardo Jun 21 '16 at 15:35
  • can you show how your database is structure. maybe a screenshot or something? – nilay neeranjun Jun 21 '16 at 23:13
  • @nilayneeranjun yeah sure. I mean I just wanted to make sure it works, so it's just keys for "title," "duration," etc. It actually works now if the user is signed in via email, it's just facebook authentication that is causing issues. `let task = ref.child("tasks").childByAutoId() task.setValue(["title": titleEntry.text!, "price": price, "duration": duration, "town_zip": townAndZip.text!])` – Brenden Belluardo Jun 22 '16 at 01:07
  • What facebook authentication? – nilay neeranjun Jun 22 '16 at 02:01
  • @nilayneeranjun currently users can either log in with Facebook or with an email address and password. For some reason, only accounts that were made via email/pass can save to the database – Brenden Belluardo Jun 22 '16 at 02:14
  • You would have to post the full code to github for me to help you with that – nilay neeranjun Jun 22 '16 at 02:15

1 Answers1

1

Did you try to check the "Rules" tab? (It's right after the "Data" tab).

enter image description here

If you have not authorised, firebase will not allow you to write data. To omit that you can changes the rules to this

{
  "rules": {
    ".read": "true",
    ".write": "true"
  }
}

But: from now on any user of your app even without authorization can modify your database, I would recommend you to use it as a temp hack.

Asalle
  • 1,239
  • 19
  • 41