Reputation: 35
Problem/Background:
When users are registered via Firebase Auth, their username, userID, and email are also being stored in Firebase Realtime Database. One of the checks in place upon user registration is to ensure that they cannot have the same username as another player, i.e. usernames are unique.
There are times when in a run/build that the Firebase Realtime Database query starts returning seemingly erratic values. When it works, it works perfectly. When it doesn't, it returns the incorrect values after the query, e.g. username "lowkeynerd" exists in the database, but the query returns an empty json. However, in that same run "highkeynerd", a different username, might return correctly so it is not consistent.
Run 1, Query working as expected, username "234" does not exist:
Run 1, Query working as expected, username "lowkeynerd" does exist:
Run 2, Query not working, username "lowkeynerd" exists but json is returning empty:
Run 2, Query working, username "highkeynerd" exists and is returning correctly:
Question:
I've been at it for a few days and am at a loss, so any help would be appreciated. Does anyone see anything worth trying or obvious that I'm missing?
Relevant Code:
public void RegisterButton() //starts first register coroutine
{
StartCoroutine(RegisterButtonEnumerator());
}
IEnumerator RegisterButtonEnumerator() //starts the coroutine to see if a username already exists, this is likely where the issue is.
{
yield return StartCoroutine(CheckIfUsernameExists((string checkedUsername) =>
{
if (checkedUsername == "False") //if username is not in use, then start register coroutine
{
StartCoroutine(RegisterLogic(registerUsername.text, registerEmail.text, registerPassword.text));
}
else if (checkedUsername == "Error") //for when firebase returns a json that contains just "{}" - unsure why this happens
{
registerOutputText.text = "Unable to query firebase successfully, try again later";
}
else //if True, username is in use
{
registerOutputText.text = "Username in use, choose a different username";
}
}));
}
IEnumerator CheckIfUsernameExists(Action<string> onCallback) //ensuring usernames are unique
{
var usernameCheck = FirebaseDatabase.DefaultInstance.GetReference("users").OrderByChild("username").EqualTo(registerUsername.text).GetValueAsync(); //this query is behaving strangely
yield return new WaitUntil(predicate: () => usernameCheck.IsCompleted);
DataSnapshot snapshotUsername = usernameCheck.Result;
if (usernameCheck.Exception != null) //basic error checking, unsure if it actually does anything here of use
{
registerOutputText.text = "Error at CheckIfUsernameExists";
}
else
{
if (snapshotUsername.Exists) //if the json is not empty, then the username is in use
{
if (snapshotUsername.GetRawJsonValue().ToString() == "{}") //sometimes the jason is returning an empty bracket json when a username does not exist, should be returning nothing
{
onCallback.Invoke("Error");
}
else
{
onCallback.Invoke("True"); //if the username is in use, i.e. it was found, return true
}
Debug.Log("the raw json to string is " + snapshotUsername.GetRawJsonValue().ToString()); //debugging the returned value
Debug.Log("the registerusername text is " + registerUsername.text);
}
else //if snapshotUsername does not exist, then no Json is returned, not even empty brackets, which means the username is not in use
//this is not always working as expected, i.e. during certain runs looking up a username returns nothing but it should because it exists
{
onCallback.Invoke("False");
Debug.Log("the registerusername text is " + registerUsername.text);
Debug.Log("the raw json to string is " + snapshotUsername.GetRawJsonValue().ToString()); //should throw a console error when working correctly (i.e. not exist)
}
}
}
Upvotes: 1
Views: 674
Reputation: 605
Firebase has had this problem for years. In the editor, it sometimes returns the correct values, and. sometimes in the next run no. The only solution is to stop the execution and restart it until Firebaase gives the correct results. I've been looking for a solution for a long time, but this is the only one that worked. On mobile devices this problem does not exist and it always works.
Upvotes: 1