Ler Ws Ler Ws - 1 year ago 136
Swift Question

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() {
// 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() {
// Dispose of any resources that can be recreated.

@IBAction func doneSave(sender: UIButton) {
let textView = bioTextView.text
self.dismissViewControllerAnimated(false, completion: nil)


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


Answer Source


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.


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


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()
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download