无法在实时数据库iOS Xcode上写入数据



注册用户后,我试图将用户的基本用户信息(如uidemail(写入实时数据库。用户正在注册,但数据未写入数据库。

func updateDb(){
let user = Auth.auth().currentUser
let uid = user?.uid ?? ""
print(uid)
let reference = Database.database().reference()
reference.child("userss").child(uid).setValue(["uid": uid, "email": email]){ (err, resp) in
guard err == nil else {
print("Posting failed : ")
print(err?.localizedDescription ?? "")
return
}
print("No errors while posting, :")
print(resp)
}
self.hideProgress()
}

我也遇到了同样的问题,尝试了很多方法,然后在pod文件中添加了pod 'Firebase/Analytics',一切都很完美。所以一定要加上它。

我的播客文件

# Uncomment the next line to define a global platform for your project
# platform :ios, '9.0'
target 'My Garage' do
# Comment the next line if you're not using Swift and don't want to use dynamic frameworks
use_frameworks!
# Pods for My Garage
pod 'Firebase/Analytics'
pod 'FirebaseUI/Auth'
pod 'Firebase/Database'
pod 'Firebase/Storage'
end

首先需要保护用户ID,以防用户ID为空时不会调用引用。

guard let uid = user?.uid else {
print("ID not found")
return
}

然后让你的参数像这个

let param: [String: Any] = ["uid": uid, "email": email]

而不是像这个那样上传值

reference.child("users").child(uid).updateChildValues(params) { (error, ref) in
if let err = error {
print(err.localizedDescription)
return
}
print("Values updated")
}

最新更新