2

So here is my Firebase structures look like:

firebase-url/users/
                 /user-id/
                      posts
                      profile
                      relations
                 /user-id/
                      posts
                      profile
                      relations

every user has his own posts, let's say there are 2 users. user A and B. if A has a relationship with B, then both could see each other posts and listen for the updates. the situation is simple when A listen to B posts, but A may has n relations with other users like B,C,D ..etc.

I checked other threads like this and this but there is no a clear simple solution for my problem, for example the first thread was about listening to constant number of childs, which is not a big problem.

how could A listen to multiple user posts updates? specially when users number is big and more than 10 or 20.

Community
  • 1
  • 1
Mohamed Ibrahim
  • 3,714
  • 2
  • 22
  • 44

2 Answers2

3

To achieve this you need to remodel your database. Here is how it can be done.

firebase-url
    |
    --- users
    |     |
    |     ---- userId_1
    |     |       |
    |     |       ---- userName: "John"
    |     |       |
    |     |       ---- userAge: 30
    |     |       |
    |     |       ---- posts
    |     |              |
    |     |              ---- post_1 : true
    |     |              |
    |     |              ---- post_2 : true
    |     |
    |     ---- userId_2
    |             |
    |             ---- userName: "Anna"
    |             |
    |             ---- userAge: 25
    |             |
    |             ---- posts
    |                    |
    |                    ---- post_3 : true
    |                    |
    |                    ---- post_4 : true
    |
    ---- posts
           |
           ---- postId_1
                   |
                   ---- postName: "post_1"
                   |
                   ---- users
                          |
                          ---- userId_1: true
                          |
                          ---- userId_2: true

In this way you can query your database very simple to display all the users that have access to a single post: firebase-url/posts/postId/users/ and also all the posts that a user can read: firebase-url/users/userId/posts/

Hope it helps.

Alex Mamo
  • 130,605
  • 17
  • 163
  • 193
  • I like your solution but that means in a way "if you can't listen multiple, then write multiple" :) – Mohamed Ibrahim May 09 '17 at 10:04
  • In Firebase the most important rule is to have your data as flatten as you can. This means that if you want to listen to multiple, group thats values under a node and listen to that particular node. You are right, in a way, you need to write multiple but this will make you app when growing work faster. – Alex Mamo May 09 '17 at 10:12
  • it seems there is no other solutions, and I have to follow this restriction. – Mohamed Ibrahim May 09 '17 at 10:33
  • This is not a restriction, this is how Firebase and other NoSQL databases generally are working. To learn more about this please read also this [post](https://www.airpair.com/firebase/posts/structuring-your-firebase-data). – Alex Mamo May 09 '17 at 10:37
  • So how can one display a list of posts she can see if there are only keys of these posts under her branch? I mean, yes it's easy for Anna to get a list of ids of the posts she can see, but how to get the actual posts to display in a list? – Salivan Mar 30 '18 at 19:02
  • @Salivan This is basically another question. In order to follow the rules of this comunity, please post another fresh question, so me and other users can help you. – Alex Mamo Mar 31 '18 at 09:19
0

Listen for value events

ValueEventListener onDataChange() Read and listen for changes to the entire contents of a path.

You can use the onDataChange() method to read a static snapshot of the contents at a given path, as they existed at the time of the event. This method is triggered once when the listener is attached and again every time the data, including children, changes. The event callback is passed a snapshot containing all data at that location, including child data. If there is no data, the snapshot returned is null.

ValueEventListener postListener = new ValueEventListener() {
    @Override
    public void onDataChange(DataSnapshot dataSnapshot) {
        // Get Post object and use the values to update the UI
        Post post = dataSnapshot.getValue(Post.class);
        // ...
    }

    @Override
    public void onCancelled(DatabaseError databaseError) {
        // Getting Post failed, log a message
        Log.w(TAG, "loadPost:onCancelled", databaseError.toException());
        // ...
    }
    };
mPostReference.addValueEventListener(postListener);
jazzbpn
  • 6,441
  • 16
  • 63
  • 99