尝试将数据追加到子值时,应用程序崩溃 [英] App crashes when trying to append data to a child value

查看:129
本文介绍了尝试将数据追加到子值时,应用程序崩溃的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我按照Firebase中的说明进行操作,但是在确认文本条目是String类型之后,我仍然遇到了崩溃。



以下是错误:
$ b


终止应用程序由于未捕获的异常InvalidPathValidation ,原因:'(child :)必须是一个非空字符串,不包含'。'''''''''''''''''''''/ $>

以下是代码:

 导入UIKit 
导入Firebase
导入FirebaseDatabase
导入FirebaseAuth

类BioEditViewController:UIViewController {

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

$ b重写func viewDidLoad(){
super.viewDidLoad()
// Do加载视图后,通常从一个笔尖的任何额外的设置。

$ b重写func viewWillAppear(animated:Bool){
let userID = FIRAuth.auth()?。currentUser?.uid
databaseRef.child(users ).child(userID!)。observeSingleEventOfType(.Value,withBlock:{(snapshot)in
//获取用户级别
let userBio = snapshot.value![userBio] as!String
self.bioTextView.text = userBio

))
}

覆盖func didReceiveMemoryWarning(){
super.didReceiveMemoryWarning()
//处理任何可以重新创建的资源。


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

$ b}
}
code>

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

解决方案

警告 避免实例化您的数据库引用超出范围的变量。原因: - 在你的范围之外,当你将一个类实例化为一个你不知道的变量,或者你的 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)
}


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.

解决方案

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

这篇关于尝试将数据追加到子值时,应用程序崩溃的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆