【问题标题】:App crashes when trying to append data to a child value尝试将数据附加到子值时应用程序崩溃
【发布时间】:2016-09-15 15:16:24
【问题描述】:

我正在按照 firebase 中显示的说明进行操作,但即使在确保文本条目是 String 类型之后,我仍然会崩溃。

这是错误:

由于未捕获的异常“InvalidPathValidation”而终止应用程序,原因:“(子:)必须是非空字符串且不包含“。” '#' '$' '[' 或 ']''

这是代码:

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)


    }
}

我只是想更新一个特定的孩子:userBio 而不会影响整个对象。

【问题讨论】:

  • 你在哪一行得到这个错误?
  • 对于 databaseRef.child("users/(user.uid)/userBio").setValue(textView)

标签: swift firebase


【解决方案1】:

警告

避免将数据库引用实例化到超出范围的变量。原因:- 在您的范围之外,当您将一个类实例化为一个变量时,您不知道您的 FIRApp 是否已经配置,或者一般来说,如果该类甚至是否已初始化。只需提供对变量的引用(!)并稍后在范围内实例化。

变化:-

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

let databaseRef = FIRDatabaseReference!

在使用它之前,只需将其初始化为:-

databaseRef = FIRDatabase.database().reference()

试试看:-

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

【讨论】:

  • 是的,有效,只需将其更改为 databaseRef.child("users/(FIRAuth.auth()!.currentUser!.uid)/userBio").setValue(textView) 就可以了。也感谢有关数据库实例化的提示。
  • 小错误:let databaseRef = FIRDatabaseReference!应该是let databaseRef : FIRDatabaseReference!
猜你喜欢
  • 1970-01-01
  • 2015-02-01
  • 1970-01-01
  • 2017-09-29
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-05-09
  • 1970-01-01
相关资源
最近更新 更多