0
votes

I'm doing a Xamarin Forms multiplatform app on Visual Studio. I work with Firebase Realtime Database and I created a nested table like that

enter image description here

I need to do create a listview where I can see every Book the user read filtered by genre.

return (await firebase
              .Child("Utenti")
              .Child(ID)
              .Child("Books")
              .OrderBy("Genere")
              .EqualTo("Gialli")
              .OnceAsync<Utente>()).Select(item => new Utente
              {

              }).ToList();

The problem is I have to update the rules of realtime database and add indexOn Genere to use EqualTo. But ID always changes, because it is user's UID.

I tried with

"Utenti": {
".indexOn": ["Genere"] }

But the App crashes and ask me to use the entire root. Any suggestions?

1
You could look at this to see if it helpful for you. - Leo Zhu - MSFT

1 Answers

1
votes

An index (and its query) are defined at a specific location, and then index (and order by) a value that is at a specific path under each child node. The child nodes of your Utenti don't have a Genere property, so the index will be empty.

To index the property in the JSON screenshot, you need an index like this:

"Utenti": {
  ".indexOn": ["Books/codice0/Genere"] 
}

With this definition each child node of Utenti will be in the index by the value of its Books/codice0/Genere property. This also immediately means that you can only have one value of Utenti, so you can't search across multiple books.

If you want to search across all Utenti across all books, you'll need to create a flat list of all books in your data structure. You'd then associate a book with a Utenti by storing the Utenti's key as a property in the book.

Also see: