0

I am writing a polling app with Firebase backend. Users answer to yes/no questions and see results. They can't see who answered the question though, except their friends. My structure:

questions
    question1:
        readable
           text: "Do you enjoy Stack Overflow?"
           yes_count: 76
           no_count: 14
        response:
            user1: yes
            user2: no
            ...
            user1000: no

users:
    user1:
        friends:
            user1
            user999

One way to do this is just calling onChildAdded on the "response" node, and do filtering on client side. This may result in downloading thousands of unnecessary UIDs. Another way is to ask server to do it. Users put requests to get friends' UIDs on a queue, backend does the filtering and puts it at a location that users can listen to. But this is a big extra load, especially considering whenever there is a new answer to a question, the backend has to check whether that new answer is from a friend for every user.

Question: is it possible to query Firebase in such a way to get only friends' UIDs at "response" node, instead of everyone's. If not, is it better for users to do filtering (and downloading thousands of un-needed UIDs), or for server to do it (huge extra load)?

Jack Guo
  • 3,959
  • 8
  • 39
  • 60

1 Answers1

1

This becomes trivial once you require that friendships are bi-directional, so also store the reverse relationship. E.g.

friends:
    user1:
        user42
        user999
    user42:
        user1
    user999:
        user1

With this bidirectional data structure, if a user posts you can simply load their friend list with /friends/$uid and loop over that. No expensive query needed.

Frank van Puffelen
  • 565,676
  • 79
  • 828
  • 807
  • Hi Frank. I edited the question, sorry about the confusion. I know how to use bidirectional data structure to find friends. I am asking should server or client do the filtering. Thanks! – Jack Guo Feb 13 '18 at 18:06
  • There is no filtering in this data structure, it's a direct lookup of the IDs and then N direct loads of the user data you need. If you're worried about the latter being slow, it's not as bad as many devs think. See http://stackoverflow.com/questions/35931526/speed-up-fetching-posts-for-my-social-network-app-by-using-query-instead-of-obse/35932786#35932786 – Frank van Puffelen Feb 13 '18 at 18:11
  • Oh, I confused direct lookup with downloads. A stupid question. Thank you! – Jack Guo Feb 13 '18 at 19:05