我正在按照 firebase 中所示的说明进行操作,但即使在确保文本条目的类型为字符串之后,我仍然遇到崩溃。
这是错误:
由于未捕获的异常“InvalidPathValidation”而终止应用程序,原因:“(child:) 必须是非空字符串且不包含“.” '#' '$' '[' 或 ']''
这是代码:
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 而不是影响整个对象。
Warning
避免实例化对超出范围的变量的数据库引用。原因:- 在你的范围之外,当你将一个类实例化为一个变量时,你不知道你的变量是否是你的FIRApp
是否已经配置,或者一般来说该类是否已经初始化。只需提供对变量的引用(!),然后在作用域中实例化即可。
改变:-
let databaseRef = FIRDatabase.database().reference()
to
let databaseRef = FIRDatabaseReference!
在使用它之前只需将其初始化为:-
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)
}
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)