I am trying to check if a username already exist in the database.
Here is the code I have written for that..
func textFieldDidEndEditing(_ textField: UITextField) {
//check if username is already taken in the database
let username = self.usernameTextField.text!
let reference = Database.database().reference().child("activeUsernames")
let query = reference.queryOrdered(byChild: "username").queryEqual(toValue: username)
let banner = StatusBarNotificationBanner(title: " The username \(username) is not available.", style: .danger)
let successBanner = StatusBarNotificationBanner(title: "The username \(username) is available.", style: .success)
query.observe(.value, with: { (DataSnapshot) in
if DataSnapshot.exists() {
print("Darn username already exists")
banner.show()
} else {
print("Yes I can use this username")
successBanner.show()
}
}, withCancel: nil)
}
So far it works however inconsistently. Sometimes if I enter a username that already exist the print statement "Yes I can use this username"
appears when it clearly is already taken, and sometimes it lets me know it is already taken. I notice I get this statement in the console
Using an unspecified index. Your data will be downloaded and filtered on the client. Consider adding ".indexOn": "username" at /activeUsernames to your security rules for better performance
I'm not sure if this is causing the inconsistency of the checking or not. However I'm not quite sure on how to change or edit the security rules efficiently.
Here is my data struct for active usernames if this helps, just displays users uids and the username associated with that uid..
- activeUsernames
6GpUz3iLwmWdDrlMtf7tT0yAULw2
username:
8sRfoqa4dKYzUKGdIZAtwE6NQZH2
username:
SRY4xUmRMgXbLL7GfsxosF6sS1y2
username:
Also here are the security rules
"rules": {
".read": true,
".write": true
}
}