简体   繁体   中英

unity - retrieving data from firebase database

I am new to firebase and somewhat new to Unity (also, this is my first stack exchange post). I know how to write to firebase, but I do not know how to retrieve from the data tree.

The way my data is structured (roughly) is as follows:

{
Users:{
    "Email": , 
    "Password":
    }
}

How would I access the elements of my users and retrieve such things as their email and password?

I'm in the same boat as you (new to both firebase and unity), but something like the following worked for me. I adapted it to your scenario without testing, so might have minor bugs but it should get you started. Perhaps someone more experienced can improve upon it. I'm intentionally sparing details on proper handling of passwords.

   //assuming
         public class User {


            public string email;
            public string password;

            public User (string email, string password) {
                this.email = email;
                this.password = password;
            }
        }

    //inside some class
        public void AddUser(){
          User user = new User("email@email.com", "password");
          string json = JsonUtility.ToJson(user);
          Firebase.Database.DatabaseReference dbRef = Firebase.Database.FirebaseDatabase.DefaultInstance.RootReference
          dbRef.Child("users").Push().SetRawJsonValueAsync(json);
        }

        public void GetUsers(){
          Firebase.Database.FirebaseDatabase dbInstance = Firebase.Database.FirebaseDatabase.DefaultInstance;
          dbInstance.GetReference("users").GetValueAsync().ContinueWith(task => {
                    if (task.IsFaulted) {
                        // Handle the error...
                    }
                    else if (task.IsCompleted) {
                      DataSnapshot snapshot = task.Result;
                      foreach ( DataSnapshot user in snapshot.Children){
                        IDictionary dictUser = (IDictionary)user.Value;
                        Debug.Log ("" + dictUser["email"] + " - " + dictUser["password"]);
                      }
                    }
          });

By creatively copy/pasting from the documentation page I get to this:

FirebaseDatabase.DefaultInstance
  .GetReference("Users")
  .ValueChanged += HandleValueChanged;
}

void HandleValueChanged(object sender, ValueChangedEventArgs args) {
  if (args.DatabaseError != null) {
    Debug.LogError(args.DatabaseError.Message);
    return;
  }
  Debug.Log(arg.Snapshot.Child("Email").Value)
}

Ik this is an old post but it might help someone,

Just call SaveAndRetrieveData() function to save and retrieve data where ever you want

public void SaveAndRetrieveData() //from the database (server)...
{
    //store the data to the server...
    reference.Child("My Car Collections").Child("Sports Car").SetValueAsync("Ferrari");
    print("data saved");
    //Retrieve the data and convert it to string...
    FirebaseDatabase.DefaultInstance.GetReference("My Car Collections").GetValueAsync().ContinueWith(task => 
    {  
        DataSnapshot snapshot = task.Result;
        string ss = snapshot.Child("Sports Car").Value.ToString();
        print(ss);
        print("data retrieved");
    });        
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM