0

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?

Doug Stevenson
  • 297,357
  • 32
  • 422
  • 441
Fio
  • 43
  • 5
  • You could look at [this](https://stackoverflow.com/questions/43260689/adding-and-indexon-value-into-firebase-rules) to see if it helpful for you. – Leo Zhu Nov 06 '20 at 07:42

1 Answers1

1

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:

Frank van Puffelen
  • 565,676
  • 79
  • 828
  • 807
  • Thank you for you answer! Problem is, codice0 always changes, just like the user's ID. I'm going to store book's key as a property of the user but I got problems with getting data so I'm just making attempts now, thank you by the way! – Fio Nov 05 '20 at 08:26