简体   繁体   中英

Firebase Database Error: System.Collections.Generic.Dictionary`2[System.String,System.Object]

Firebase Databse Fails to download data when needed in between other similar operations in app.

System.Collections.Generic.Dictionary`2[System.String,System.Object]

Firebase Cannot take do new GetValueASync sometimes in middle of similar operations of app and throws System.Collections.Generic.Dictionary 2[System.String,System.Object]` as a snapshot value.

Steps to reproduce: Setup any unity project with realtime databse perform non stop multiple times: .GetValueAsync().ContinueWithOnMainThread(task => then you will get value as System.Collections.Generic.Dictionary 2[System.String,System.Object]` instead of any child/key/database value.

public void aaaa() {
    Reference.Child("Users").OrderByChild("About/XP").StartAt(1).LimitToFirst(12).GetValueAsync().ContinueWithOnMainThread(task => {
      if (task.IsFaulted) {

        return;
      } else if (task.IsCompleted) {
        DataSnapshot Snapshot = task.Result;
        if (Snapshot != null)
          Debug.Log(Snapshot.Value);
        return;
      }
      return;

    });

Well this happens if you use ToString (which is what Debug.Log does internally) on a dictionary or in general any object of a type that doesn't implement it explicitly, it simply returns the same as GetType().FullName .

Default implementations of the Object.ToString method return the fully qualified name of the object's type.

So it looks like the type is a Dictionary<string, object> .

If you want to see all items you rather want to do eg

foreach(var kvp in Snapshot.Value)
{
    Debug.Log($"Key: {kvp.Key}, Value: {kvp.Value}");
}

Note though: while the key is a string , the value once again might be a type not implementing ToString in which case again it will simply print out the type name.


If you really want to print out the entire structure (as far as the values are serializable) you could use Newtonsoft JSON.Net and convert the entire dictionary into a human readable JSON format.

See Serialize a Dictionary

string json = JsonConvert.SerializeObject(Snapshot.Value, Formatting.Indented);
Debug.Log(json);

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