Reputation: 21
I am working on building out an authentication system for a project I am working on.
Part of this is to save UserData locally so that it doesn't have to connect to cloud on every run, or force a login ETC.
The problem i am having, is when i am trying to set PlayerPrefs it is not working. I have it running a Debug.Log before each PlayerPrefs.SetString and it only ever displays the first one.
I am able to do PlayerPrefs.SetString in my Start() but for some reason it is not working here and i have no idea why.
Everything works up until i am saving to PlayerPrefs
public Dictionary<string, string> LoggedUser = new Dictionary<string, string>();
public void getUserData(string id, string email)
{
Debug.Log("Getting User Data for ID: " + id);
FirebaseDatabase.DefaultInstance
.GetReference(id)
.GetValueAsync().ContinueWith(task1 =>
{
if (task1.IsFaulted)
{
Debug.Log("Failed");
}
else if (task1.IsCompleted)
{
Debug.Log("Data Retrieved");
DataSnapshot snapshot = task1.Result;
foreach (DataSnapshot user in snapshot.Children)
{
string firstName = user.Child("firstName").Value.ToString();
string lastName = user.Child("lastName").Value.ToString();
string sex = user.Child("sex").Value.ToString();
string country = user.Child("country").Value.ToString();
string age = user.Child("age").Value.ToString();
string userName = user.Child("userName").Value.ToString();
Debug.Log(firstName);
LoggedUser.Add("FirstName", firstName);
LoggedUser.Add("LastName", lastName);
LoggedUser.Add("Email", email);
LoggedUser.Add("age", age);
LoggedUser.Add("sex", sex);
LoggedUser.Add("country", country);
LoggedUser.Add("userName", userName);
Debug.Log("Welcome " + LoggedUser["FirstName"]);
Debug.Log("User signed in successfully: " +newUser.Email +" " +newUser.UserId);
setPlayer();
}
}
});
}
public void setPlayer()
{
Debug.Log("Saving to PlayerPrefs");
Debug.Log("Setting First Name");
PlayerPrefs.SetString("FirstName", "Ezekiel");
Debug.Log("Setting Last Name");
PlayerPrefs.SetString("LastName", LoggedUser["LastName"]);
Debug.Log("Setting Email");
PlayerPrefs.SetString("Email", LoggedUser["Email"]);
Debug.Log("Setting Age");
PlayerPrefs.SetString("age", LoggedUser["age"]);
Debug.Log("Setting Sex");
PlayerPrefs.SetString("sex", LoggedUser["sex"]);
Debug.Log("Setting Country");
PlayerPrefs.SetString("country", LoggedUser["country"]);
Debug.Log("Setting userName");
PlayerPrefs.SetString("userName", LoggedUser["username"]);
Debug.Log("Setting Loggedn State");
PlayerPrefs.SetInt("LoggedIn", 1);
Debug.Log("Saving Player Prefs");
PlayerPrefs.Save();
Debug.Log("Welcome " + PlayerPrefs.GetString("username"));
}
Upvotes: 0
Views: 763
Reputation: 3131
If you don't mind, I can expand on your answer a little bit.
If you use ContinueWithOnMainThread instead of ContinueWith, your continuation logic automatically occurs on the main thread (so you can write PlayerPrefs
). I cover a whole ton of ways to handle threading with Firebase and Unity in this article if you'd like to compare them.
Furthermore, this is a stab in the dark, but if you're using Realtime Database then you may also be using Firebase Authentication. If this is the case, then FirebaseAuth.DefaultInstance.CurrentUser actually persists between runs of your game (I cover this a little bit in this video). Since Realtime Database also caches data you request locally, you may actually be doing extra work that the Firebase plugins will handle for you!
Upvotes: 2
Reputation: 21
I was able to find the solution.
getUserData() method is running async, which creats a new thread. the PlayerPrefs can only run on a main thread.
My solution was to create a boolean that i flippd to true when the Data was loaded into LoggedUser. my Update() method was watching for that to flip to true, where it then called the setPlayer().
Than you all for the help!
Upvotes: 2
Reputation: 371
You are setting userName
PlayerPrefs.SetString("userName", LoggedUser["username"]);
and retrieving username
Debug.Log("Welcome " + PlayerPrefs.GetString("username"));
Also it should be
PlayerPrefs.SetString("userName", LoggedUser["userName"]);
instead of
PlayerPrefs.SetString("userName", LoggedUser["username"]);
Upvotes: 0