2017-08-07 82 views
1

我是firebase的新手,對Unity來說有點新鮮(另外,這是我的第一個堆棧交換帖子)。我知道如何寫入firebase,但我不知道如何從數據樹中檢索。統一 - 從Firebase數據庫中檢索數據

我的數據結構化的方式(大致)如下:

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

我怎麼會進入我的用戶的內容和檢索這樣的事情作爲自己的電子郵件地址和密碼?

+0

這應該很好地解釋它:https://firebase.google.com/docs/database/unity/retrieve-data#retrieving_data。只需將'Leaders'替換爲節點的名稱即可。 –

+0

您好弗蘭克。我仍然有點困惑。我將如何遍歷我創建的密鑰並訪問所述密鑰的電子郵件和密碼元素? –

回答

0

通過創造性複製/從documentation page粘貼我得到這個:

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) 
} 
+0

這將允許我遍歷數據庫並檢查用戶?當我把它放在我的代碼中時,它顯示的是我的控制檯窗口中對我的數據庫所做更改的字典。 –

+0

你只寫一個sucker到數據庫,所以這段代碼顯示那個用戶。編寫用戶列表涉及調用'Push()',如下所示:https://firebase.google.com/docs/database/unity/save-data#append_to_a_list_of_data –

+0

是的,我有。我有什麼麻煩的理解是這樣的:我有一個數據庫與多組數據,我想遍歷該數據並檢查該數據中的用戶帳戶。上面給出的代碼示例顯示了添加新數據時所做的更改。谷歌文檔並沒有給我一個很好的理解,我可以遍歷我的數據列表,並比較我的電子郵件條目。 –

1

我在同一條船上,你(新雙方火力點和統一),但類似下面的工作對我來說。我在沒有測試的情況下將其適用於您的場景,因此可能會出現一些小錯誤,但應該讓您開始。也許更有經驗的人可以改進它。我故意避免正確處理密碼的細節。

//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 protected]", "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"]); 
         } 
        } 
      }); 
相關問題