Ler Ws
Ler Ws

Reputation: 317

App crashes when trying to append data to a child value

I'm following the instructions as shown in firebase but I'm still getting crashes even after making sure that the text entry is type String.

Here's the error:

Terminating app due to uncaught exception 'InvalidPathValidation', reason: '(child:) Must be a non-empty string and not contain '.' '#' '$' '[' or ']''

and here's the code:

import UIKit
import Firebase
import FirebaseDatabase
import FirebaseAuth

class BioEditViewController: UIViewController {

    @IBOutlet weak var bioTextView: UITextView!
    let databaseRef = FIRDatabase.database().reference()


    override func viewDidLoad() {
        super.viewDidLoad()
        // Do any additional setup after loading the view, typically from a nib.
    }

    override func viewWillAppear(animated: Bool) {
        let userID = FIRAuth.auth()?.currentUser?.uid
        databaseRef.child("users").child(userID!).observeSingleEventOfType(.Value, withBlock: { (snapshot) in
            // Get user level
            let userBio = snapshot.value!["userBio"] as! String
            self.bioTextView.text = userBio

        })
    }

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }


    @IBAction func doneSave(sender: UIButton) {
        let textView = bioTextView.text
        self.dismissViewControllerAnimated(false, completion: nil)
        databaseRef.child("users/(user.uid)/userBio").setValue(textView)


    }
}

I'm just trying to update a specific child: userBio and not affect the entire object.

Upvotes: 3

Views: 6187

Answers (1)

Dravidian
Dravidian

Reputation: 9945

Warning

Avoid instantiating your database reference to a variable out of scope. Reason why :- Outside your scope, when you instantiate a class to a variable you don't know wether or not your FIRApp has already been configured or not, or in general if that class has even been initialised as of yet or not. Just provide a reference(!) to the variable and instantiate later in a scope.

Change:-

let databaseRef = FIRDatabase.database().reference()

to

let databaseRef = FIRDatabaseReference!

And before using it just initialise it as:-

databaseRef = FIRDatabase.database().reference()

Try :-

 @IBAction func doneSave(sender: UIButton) {
    let textView = bioTextView.text
    self.dismissViewControllerAnimated(false, completion: nil)
    databaseRef = FIRDatabase.database().reference()
     databaseRef.child("users/\(FIRAuth.auth!.currentUser!.uid)/userBio").setValue(textView)
      }

Upvotes: 5

Related Questions