M. Kremer M. Kremer - 11 months ago 67
Swift Question

Firebase using queryEqualTo() two times

I have the following data structure in my database:

enter image description here

I do understand how to query for questions in one language like de:

let ref = FIRDatabase.database().reference().child("madeGames")
ref.queryOrderedByChild("de").queryEqualToValue(true).observeSingleEventOfType(FIRDataEventType.Value) { (snap: FIRDataSnapshot) in
print("Data:", snap.value)


It seems to be impossible for me to just get games in de AND en from the server.

Of course I know I can first download all the de question and then loop throw them offline but that doesn't seem to be very intelligent for me.

Does anyone know how to do that more efficiency?

Jay Jay
Answer Source

One possible option is to change your structure by combining the children properties into a single property like this.

      de_en: true
      de_fr: true
      de: true

If you have a lot of languages you could also do this:

      language_available: 0   //just de
      language_available: 3   //de_en

    0: de
    1: en
    2: fr
    3: de_en
    4: de_en_fr

Edit - and another option I just thought of... go binary!

00000001 = en
00000010 = fr
00000100 = de
00001000 = du
00010000 = ge

Then to get any combination of languages, perform a bitwise 'or' operation on the two (or more) you want.

So to get en_de, it's 00001001. To get en_du_ge it would be 00011001. If you have 8 languages, that could be represented by 8 bits and 256 combinations. So if you have 11 languages, that would be 2^11 = up to 4096 combinations.

Using this technique you could avoid storing the en_fr etc totally and just search for the bitwised value.

You could store decimal versions of the binary numbers in Firebase... just showing the binary here for clarity.