我開始研究 iOS 開發幾個星期,在創建我的第一個應用程式時,我不得不解決“用戶授權”的實作問題
出于這些目的,我選擇了 Firebase
然后就出現了這樣的問題,因為在注冊用戶的時候,我保存了他的郵箱并登錄到資料庫中,需要設定一個方法,當你點擊“注冊”按鈕時,會檢查這樣的郵箱和login不在資料庫中,然后才繼續。
在研究了 Firebase 檔案并觀看了有關此服務的一些視頻后,我嘗試按如下方式解決此問題:
private func isNewEmail(_ email: String, completion: _ emailIsNew: Bool -> ()) {
ref = Database.database().reference(withPath: "users")
ref.getData { error, snapshot in
var emailIsNew: Bool = true
guard error == nil else { return }
guard let snapshotValue = snapshot.value as? [String : AnyObject] else {
if snapshot.value as? [String : AnyObject] == nil {
completion(emailIsNew)
return
}
return
}
for item in snapshotValue {
let itemValueDictionary = item.value
guard let emailFromDatabase = itemValueDictionary["email"] as? String else { return }
if email.lowercased() == emailFromDatabase.lowercased() {
emailIsNew = false
break
}
}
completion(emailIsNew)
}
}
接下來,我們呼叫上述方法,并將電子郵件傳遞到那里,根據 emailIsNew 的值,我們要么創建用戶,要么不創建用戶。
The most important problem: I assumed that if we have, for example, 10,000 users in our database, then such a check can take a very long time, it seems to me that when a person clicks "Register" and then waits 10 minutes for the application to check everything - this is unacceptable, so I tried to find another way to solve the original problem, but unfortunately, I could not find it due to, I suppose, a small amount of experience. I ask you to suggest how to solve this problem, how you can change the verification method, or in general, perhaps, apply something else.
MARK - 我在 stackoverflow 上研究了類似的答案,但其中大部分與 android 或 Java 相關,或者我無法應用解決方案來解決我的問題。因為我只是在學習英語,所以也許這就是我找不到答案的原因,但是,我仍然希望收到對我的方法的評論,以及指向類似問題的鏈接。謝謝你的理解。
uj5u.com熱心網友回復:
如果你的每個孩子users都有一個email你想要檢查的值的屬性,你可以使用查詢來檢索匹配的節點。
就像是:
var query = ref.queryOrdered(byChild: "email").queryEqual(toValue: email.lowercased())
query.getData { error, snapshot in
...
由于 Firebase 中的所有搜索都區分大小寫,因此這確實要求您的資料庫已經包含所有小寫的電子郵件地址。如果不是這種情況,請考慮添加email_lowercase具有小寫值的屬性(例如)并在上面的查詢中使用它。
轉載請註明出處,本文鏈接:https://www.uj5u.com/ruanti/360598.html
