I need to show feeds in my app and want to update UI real-time same like Twitter app?
As per my understandings, Below are the few ways,
- I can do this by continuous background call to API from service and update UI using handlers.
- I can register broadcast receiver and fire them once updated data is available and then update UI if the user is still on the same page.
However, I need to find the best way except this if possible because in future it can increase traffic of users/feeds.
2
Answers
Don’t use this method, continuous background call of an API will reduce the performance.
When to send the broadcast?
For this, you can get push notification from backend which tells your app that updated data is available and you can continue with your BroadcastReceiver
Overall 2nd will be the option instead of 1st.
You can use Firebase Cloud Messaging api to get the notification that your database has changed. In this case, the notification will be of type ‘data’. Once you get the notification from FCM, you need to handle your logic in
onMessageReceived()
method. From there you can useEventBus
orBroadcastReceiver
whichever suits your requirements.