-
Notifications
You must be signed in to change notification settings - Fork 53
Why using EventBus when Realtime database is used #2
Comments
EventBus is used to send a notification event from MyFirebaseMessagingService.java to ChatFragment.java. The main reason behind it is that Realtime database cannot function when the application is not running on user's device. So whenever a notification is received and clicked, the application automatically sends the user to the chat screen with the help of EventBus. A local broadcast receiver can also be used instead of EventBus, but EventBus is much more easier to implement, that's why I went with it. |
On click of notification pendingIntent would open the specific activity, then how eventBus is used here. i don't get it please tell me |
Oops sorry, I gave the wrong explanation without properly checking out my code. Yes, you are right about using Realtime database in this scenario. As you can see in the code below, I only send the events when chat activity is opened.
The main reasoning behind EventBus usage is that there was one case generating where I wasn't receiving messages, which I will explain using this simple example:
But, this generates an issue, i.e., now everytime receiver will update the chat messages twice, which I think I overlooked before. So, thanks for asking this question. If you have any idea how I should fix this that would be extremely helpful. I would re-open this issue and try to fix it later :). Fixing this issue might generate some performance improvements too. |
I think that the getMessages method won't be called twice as I have put a check above it, still it is a wrong pattern. |
If we are using firebase realtime database it will automatically update the new message in onDataChanged() method if the chatActivity is open, then why the need of eventBus there to pass that a new message came
The text was updated successfully, but these errors were encountered: