step-up-labs / firebase-database-dotnet

C# library for Firebase Realtime Database.
MIT License
668 stars 168 forks source link

Inconsistency in using elementRoot parameter? #235

Closed rythos42 closed 3 years ago

rythos42 commented 4 years ago

I have a JSON tree like:

{
  "Battle": [
    "id1": {
      "somevalue": 1
    },
    "id2": {
      "somevalue": 2
    }
  ]
}

When trying to listen to changes on the sub-tree of "id1", I started with the following code:

  return firebase
    .Child($"Battle/{battleId}")
    .AsObservable<BattleData>(delegate (object sender, ContinueExceptionEventArgs<FirebaseException> z)
      {
        Console.WriteLine(z.ToString());
      });

What happens with this, is that the REST API call sends a path of "/" back on my subscribe, which made the code in FirebaseCache skip lines 60 to 99, treated the object as a dictionary and tried to parse using GetObjectCollection on line 119, which failed because my object is a complex type, not a dictionary of that type.

Reading through code, I realized I needed to use the elementRoot parameter. I started with it as a value of Battle/id1. (I used a parameter, but this string is more useful for the example). This made FirebaseCache go into lines 60-99, but ultimately failed on line 85, because there is no property id1 on my type BattleData, that is the ID of my object.

What worked was using elementRoot of id1. This goes into lines 60-99, and leaves the private variable obj as my class. This starts to treat FirebaseCache as just a cache of BattleData objects though, which doesn't look like it's intended purpose (although it's fine for me), and I'm not sure if this works for other scenarios?

I'm writing an issue for discussion, maybe leading to me understanding that I'm wrong, which has led to an inconsistency. :) Or maybe others using this library have a similar problem. (or maybe they needed to have the ContinueExceptionEventArgs parameter pointed out, which was the pin that cracked this case open for me...no errors were coming out of the observable, which was maddening...)

stale[bot] commented 3 years ago

This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. Thank you for your contributions.

stale[bot] commented 3 years ago

Closing the issue due to inactivity. Feel free to re-open