0

I have a Firebase database that consists of a child "messages" in which each message is stored.

I want to only retrieve the values where the senderID or ReceiverID is equal to the senderID or Receiver ID in the app. This way i can sort out messages per user.

However, I am not sure about doing this locally on the app or from Firebase using a query. If that is possible?

This is the Database

AL.
  • 36,815
  • 10
  • 142
  • 281
  • You can [use Firebase queries to filter](https://firebase.google.com/docs/database/ios/lists-of-data#sorting_and_filtering_data). But queries you can only filter on a single property, so *either* senderID *or* receiverID. It seems that you're building a chat room, in which case I'd model the data like in [my answer here](http://stackoverflow.com/q/33540479). I recommend reading [NoSQL data modeling](https://highlyscalable.wordpress.com/2012/03/01/nosql-data-modeling-techniques/) and viewing [Firebase for SQL developers](https://www.youtube.com/playlist?list=PLl-K7zZEsYLlP-k-RKFa7RyNPa9_wCH2s) – Frank van Puffelen Apr 16 '17 at 07:09
  • So, basically you want to get all the messages whose sender is the receiver as well? If so, I recommend doing it after you retrieve your data. – Mr. Xcoder Apr 16 '17 at 07:11
  • Well, is their any other way like e.g i need to group messages per user. Like UserA Sender and UserB Receiver so group these messages and make them available to senderA and B only. – Hussain Mustafa Apr 16 '17 at 08:03
  • This is tough to answer because it depends on your use case. For example, say user with uid 15D... is logging in. Typically, upon login, that user would attach an observer (a query) to the messages node that would notify them if a message containing their receiver ID was posted. They would be notified immediately if another user posts to the messages node where receiverID = 15D... in that case and the data in the node would be delivered to the app; they would then know who the senderID was. – Jay Apr 16 '17 at 12:23

1 Answers1

0

This is where Firebase lacks and it is not their problem since the product is not a database. It's just a json file. How I would approach this is as follows:

Messages
  |- receiverID
       |- senderID
            |- The rest of your dictionary

That was you can access a child without even querying for it. You could just say:

child('Messages').child(receiverID).child(senderID).observe value etc.

I know it's ugly, but it is a working solution. Hope it helps!

DanielEdrisian
  • 716
  • 1
  • 4
  • 17
  • Thanks @SahandTheGreat, – Hussain Mustafa Apr 17 '17 at 05:07
  • But by this only the sender will observe for the messages and not the receiver. Then we would have to copy the same message into the receivers node i guess. – Hussain Mustafa Apr 17 '17 at 07:43
  • I think you can listen for any changes under a child. So for example, the receiver could be listening to his own child, waiting for new senders. If there is a new message, a new sender child will be added. Great, now you can find new messages. You can also listen for when a child under receiver changes, so if the same sender sent a new message, you would be able to find it. – DanielEdrisian Apr 17 '17 at 13:03