如何解决如何创建闭包以检查Firebase中的文档是否已经存在?
我有一个函数可以生成随机令牌(“ WE4R5”,“ XQ895”等),并在Firestore中使用此令牌的名称创建文档。
我希望能够在返回令牌之前检查生成的令牌是否已经存在。
如果确实存在->重新生成令牌并再次检查 如果没有->将其退回
class Generatetoken: UIViewController {
let db = Firestore.firestore()
var alphaBet = ["1","2","3","4","5","6","7","8","9","0","A","B","C","D","E","F","G","H","I","J","K","L","M","N","O","P","Q","R","S","T","U","V","W","X","Y","Z","1","0"]
func generatetoken() -> String {
var token = [String]() // Initialize an empty array
alphaBet.shuffle()
for i in 0...4 {
token.append(alphaBet[i]) // Appending characters to the array
}
let generatedToken = token.joined(separator: "") // Creating a string from the characters in the array
return generatedToken
}
func checkIfTokenExists(generatedToken: String,delegate: @escaping (_ token: String) -> Void) {
db.collection("Rooms").document(generatedToken).getDocument { (documentSnapshot,error) in
if let err = error {
print(err.localizedDescription)
} else {
if let document = documentSnapshot,document.exists {
//If document already exists,Regenerate token and check again
} else {
//If document does not exist,pass the generated token back to generatetoken() which will return it
let token = generatedToken
delegate(token)
}
}
}
}
我找不到如何成功实现此目标的方法,下一步该怎么做?
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。