如何保存到Firebase-Swift [英] How to save to Firebase - Swift
本文介绍了如何保存到Firebase-Swift的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
可能我弄错了.
这是我尝试在fb身份验证后存储用户数据的方法:
This is how I try to store user data after fb auth:
func loginButton(loginButton: FBSDKLoginButton!, didCompleteWithResult result: FBSDKLoginManagerLoginResult!, error: NSError?) {
print("sds")
print(result)
if let error = error {
print(error.localizedDescription)
return
}
let credential = FIRFacebookAuthProvider.credentialWithAccessToken(FBSDKAccessToken.currentAccessToken().tokenString)
FIRAuth.auth()?.signInWithCredential(credential) { (user, error) in
if let email = user?.email
{
self.ref.child("users").child(user!.uid).setValue(["email": email])
}
if let photoURL = user?.photoURL?.absoluteString
{
self.ref.child("users").child(user!.uid).setValue(["photoURL": photoURL])
}
}}
当我在控制台中查看数据库时,我得到的仅仅是photoURL,看起来就像是在删除/覆盖后立即创建和创建电子邮件一样.
What I get when I look into database in console is only the photoURL, looks like email is created and immmediately after deleted/overriden.
将数据保存到Firebase的正确方法是什么?
What's the correct way to save data to firebase?
推荐答案
设置值时,将覆盖该路径上的所有内容.
When you set value you are overwriting everything at that path.
如果您只想替换某些值,则应使用:
If you only want to replace certain values you should use:
self.ref.child("users").child(user!.uid).child("photoURL").setValue(photoURL)
或替换/创建整个用户对象
Or replace/create entire user object
self.ref.child("users").child(user!.uid).setValue(["email": email, "photoURL": user?.photoURL?.absoluteString ?? ""])
这篇关于如何保存到Firebase-Swift的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文