Swift DocumentID在firebase中每次都在更新
我是firebase的新手,在主视图上与gmail一起唱歌。在用户唱歌之后,我根据documentId保存userinfo,如果用户已经存在(已经使用gmail登录),则无需保存信息,只需更新已存在的信息。所以我尝试了一些代码,但是我没有达到我所期望的Swift DocumentID在firebase中每次都在更新,swift,firebase,google-cloud-firestore,Swift,Firebase,Google Cloud Firestore,我是firebase的新手,在主视图上与gmail一起唱歌。在用户唱歌之后,我根据documentId保存userinfo,如果用户已经存在(已经使用gmail登录),则无需保存信息,只需更新已存在的信息。所以我尝试了一些代码,但是我没有达到我所期望的 func sign(_ signIn: GIDSignIn!, didSignInFor user: GIDGoogleUser!, withError error: Error!) { if let error = error
func sign(_ signIn: GIDSignIn!, didSignInFor user: GIDGoogleUser!, withError error: Error!) {
if let error = error{
print(error.localizedDescription)
return
}
guard let authentication = user.authentication else { return }
let credential = GoogleAuthProvider.credential(withIDToken: (authentication.idToken)!, accessToken: (authentication.accessToken)!)
Auth.auth().signIn(with: credential, completion: { (user, error) in
if let error = error {
print("Login erro \(error.localizedDescription)")
return
}
在这里,我检查firestore中的所有文档id
self.db.collection("pruser").getDocuments() { (querySnapshot, err) in
if let err = err {
print("Error getting documents: \(err)")
} else {
for document in querySnapshot!.documents {
// print("\(document.documentID) => \(document.data())")
print("\(document.documentID)")
在这里,我必须检查现有的文档id和当前用户
文档id。如果已存在更新信息,如果未设置文档id上的值,请尝试使用此代码
self.ref = self.db.collection("pruser").document()
let doc = self.ref?.documentID
print("printdoucment id ",doc!)
}
}
}
}
用户id:VNRSXNJJEPFWRCAWOLLY1IBYHV352Doucment id:PKKLapD9xE9kNNvntI8X 用户id:VNRSXNJJEPFWRCAWOLLY1IBYHV352
Doucment id: PXJIVxegbzgfk3mijk5
当我使用现有电子邮件登录时,documentid也不一样,每次登录时documentid都不一样。如何实现此问题 尝试将其添加到viewDidLoad(): 并将7-11管路更换为:
Auth.auth().signIn(with: credentials) {
(authResult, error) in
if let error = error {
print(error.localizedDescription)
}else{
print("Log in successful")
这应该在用户中登录
至于DocumentID,我认为在代码中没有将每个用户链接到每个DocumentID。您需要根据用户ID更改文档ID或读取文档ID。如果没有参数,则每次调用它时都会生成一个新的文档ID。如果需要的话,必须向其传递一个参数来指定文档ID
self.db.collection("pruser").document(user.uid)
你有没有想过改用实时数据库?我发现它对初学者更友好,使用起来更清晰。@HubertRzeminski感谢你用云商店回复我的要求。
self.db.collection("pruser").document(user.uid)