Home Firebase UI RecyclerView - Handling mapping
Reply: 1

Firebase UI RecyclerView - Handling mapping

Divyansh Singh
1#
Divyansh Singh Published in 2018-02-14 09:21:46Z

Below is my sample structure:

channels
    |
    |____channel0
               |
               |___messages
                     |
                     |_______ uniqueKey0: true
                     |
                     |_______ uniqueKey1: true   


 messages
   |
   |_____ uniqueKey0
   |          |______ from: Alex
   |          |______ text: Hi!
   |
   |
   |_____ uniqueKey1
             |______ from: Kate
             |______ text: Hello

I am trying out Firebase UI Recycler View to messages. I have two questions:

  1. What I am doing is giving ref of /channels/messages to FirebaseAdapter and get the key. Once key is fetched, I fetch details and then populate the view holder. In this case, am not sure how to handle the case where a message's text is changed. Because since am listening to /channels/messages, I don't get the event of message change.

  2. To tackle the above problem, I was thinking of the following structure, which I know is against the best practices since am supposed to keep data as flat as possible. So I wanted I know that going for this approach is really that bad or makes more sense in this case?

     messages
       |
       |____channel0
             |_____ uniqueKey0
             |          |______ from: Alex
             |          |______ text: Hi!
             |
             |
             |_____ uniqueKey1
                        |______ from: Kate
                        |______ text: Hello
    
Peter Haddad
2#
Peter Haddad Reply to 2018-02-14 09:29:39Z

Yes u can use this approach:

 messages
 |
 |____channel0
        |_____ uniqueKey0
        |          |______ from: Alex
        |          |______ text: Hi!
        |
        |
        |_____ uniqueKey1
                  |______ from: Kate
                  |______ text: Hello

then to get the you can do this:

DatabaseReference ref=FirebaseDatabase.getReference().getInstance().child("messages").child("channel0").orderByChild("from").equalTo(Alex);

you will then be able to get the text: hi!

And the data structure above is actually flat, it is like this:

   messages
       channel0
             randomid
                keys:values
             randomid
                keys:values

This is not flat:

        messages
          channel0 
            channel1
              channel2
                 randomid
                    keys:values
                    name
                      username:peter
                 randomid
                    keys:values
                    name
                      username:peter

and so on..

All you have to do when structuring the database, is think about the queries. If i did this or that, is it easy to query?Is it possible to query?

You need to login account before you can post.

About| Privacy statement| Terms of Service| Advertising| Contact us| Help| Sitemap|
Processed in 0.353249 second(s) , Gzip On .

© 2016 Powered by mzan.com design MATCHINFO