gpt4 book ai didi

ios - DispatchGroup 仅当方法被调用两次时才会退出

转载 作者:行者123 更新时间:2023-11-30 11:28:54 24 4
gpt4 key购买 nike

我正在尝试使用 Firebase 身份验证来注册用户。当用户注册时,我希望将他们添加到我的 Users 中Firestore 中的集合以及 Users授权部分。

createUser(withEmail: ...)方法每次都有效。然而,我的db.collection("users").document(user.id).setData([..]仅当我按下注册按钮两次时才会调用方法,此时 createUser(withEmail ...)方法再次被调用。相关代码如下

SignupViewController.swift

@IBAction func signupButtonTapped(_ sender: UIButton) {
// user: User() defined here

usersHelper.signup(user: user, password: password) { result in
// This closure is only executed on the second press
guard let user = result as? Firebase.User else {
let error = result as? Error
self.handleSignupError(error!)
return
}
self.performSegue(withIdentifier: "ShowGroupsFromSignupSegue", sender: self)
}
}

UsersHelper.Swift

func signup(user: User, password: String, completion: @escaping (_ result: Any?) -> Void) {
let userDispatchGroup = DispatchGroup()
var signupError: Error? = nil
var dbError: Error? = nil
var firebaseUser: Firebase.User? = nil

userDispatchGroup.enter()
usersDataModel.signupUser(user: user, password: password) { result in
// Completion handler
if result as? Error != nil {
signupError = result as? Error
} else {
// Got the user
firebaseUser = result as? Firebase.User
}
userDispatchGroup.leave()
}

userDispatchGroup.enter()
usersDataModel.create(user: user) { err in
// This will only execute if signUp is called twice
if let result = err as? Error {
print("Error msg: \(result.localizedDescription)")
dbError = result
}
print("!Created db user")
userDispatchGroup.leave()
}

userDispatchGroup.notify(queue: .main) {
print("!dispatch group completed successfully")
if (signupError == nil && dbError == nil) {
completion(firebaseUser)
} else {
signupError != nil ? completion(signupError) : completion(dbError)
}
}
}

UsersDataModel.swift

func signupUser(user: User, password: String, _ completion: @escaping (_ err: Any? ) -> Void) {
// Create user in Auth & create DB entry
Auth.auth().createUser(withEmail: user.email, password: password) { (authResult, err) in
if let err = err {
print("Error creating user \(err)")
completion(err)
} else {
print("User signed up successfully")
completion(authResult) // completion called with User
}
}
}


func create(user: User, _ completion: @escaping (_ result: Any?) -> Void) {
// userData dictionary created here

db.collection("users").document(user.ID).setData(userData) { err in
if let err = err {
print("There was an error creating the user \(err)")
completion(err)
} else {
print("!User created in db successfully!")
completion(nil)
}
}
}

非常感谢任何帮助!先谢谢大家了

最佳答案

我已经解决了这个错误。我最终嵌套了第二个网络调用,以便:

  1. 从经过身份验证的 firestore 获取 uid

  2. 不违反有关在没有授权 uid 的情况下写入数据库的 Firestore 规则

我的 UsersHelper.swift 文件现在看起来像

func signup(user: User, password: String, completion: @escaping (_ result: Any?) -> Void) {
let userDispatchGroup = DispatchGroup()
var signupError: Error? = nil
var dbError: Error? = nil
var firebaseUser: Firebase.User? = nil

userDispatchGroup.enter()
usersDataModel.signupUser(user: user, password: password) { result in
// Completion handler
if result as? Error != nil {
// there was an error?
print("Error: \(result)")
signupError = result as? Error
} else {
// Got the user
firebaseUser = result as? Firebase.User
// Create user entry in DB
user.ID = firebaseUser!.uid

self.usersDataModel.create(user: user) { err in
// Completion handler
if let err = err as? Error {
dbError = err
}
userDispatchGroup.leave()
print("Done")
}
}
}

userDispatchGroup.notify(queue: .main) {
print("!dispatch group completed successfully")
if (signupError == nil && dbError == nil) {
completion(firebaseUser)
} else {
signupError != nil ? completion(signupError) : completion(dbError)
}
}
}

关于ios - DispatchGroup 仅当方法被调用两次时才会退出,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50459017/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com