RealtimeDB DataChanged Refresh Issue when reopen screen

Hi Thunkers!

I’m having a problem when refreshing a dashboard in my home screen.

I put the listener on Screen start that calls the action to be performed. I also put a call to perform the action when the screen opens or reopens.

When the app starts, If change the data (directly in Firebase console for example), the “data changed” block works perfectly. But If I move to another screen, and return, the data changed does not work anymore. Only if I restart the app.

Below the blocks of my app with some comments.

Any idea why the refresh stops to work when I return to the 1st screen?

1 Like

I did not understand your issue.

The screen Opens block calls your function which only reads from the database and does not change anything in it so it will not activate the Changed event.

How are you testing the Data Changed if working or not?

@muneer I’m changing the data directly in Firebase console in parallel to my app, just to simulate another user changing the data at same time.

Let me try explain the order the things happens:

  1. I start the app, and it loads correctly the data from Firebase and update the dashboard at a screen called “home”. My dashboard is just a list of labels that loads directly from Firebase. And from this screen I eventually jump to other screens, and return later to this home screen.

  2. When in the home screen, if I manipulate the data directly below the key “dashboard” Firebase console, the blocks that updates my dashboard works instantly. I can change 100 times and the data is updated, that means the “Realtime datachanged” block works.

  3. So when I navigate to another screen, and change the data via the new screen, when I return to the home screen, the data is updated ok (because I have the “when open” block that force the dashboard to update). But…

  4. After navigate to another screen, and returns to the home screen the “data changed” will not trigger the blocks that updates the dashboard when I manipulate the data via Firebase anymore.

Hope to helped you to understand the issue.

Basically:

When in Home screen 1st time >> update data works anytime via Firebase

When in Home screen >> go to another screen and update the data >> when return to home screen data is updated

Home screen >> go to another screen and update data >> return to the home screen and update data >> in home screen update data in firebase directly does not work anymore.

1 Like

Thank you for the detailed explanation.

I can tell you that will not work for two reasons.

1- Starts and Opens
These names are confusing, actually they work different that what people think. One thing for sure that if you go back to your screen using a button which uses the Navigate to screen block then both Starts and Opens will execute. the only time the Starts will not execute is when you go back to the screen using the screen swipe gesture.
To overcome this issue create an app variable for example Listener Active and set it to FALSE whenever you have you screen active first check if this variable is false to to execute the listener block and immediately set this variable to TRUE. This way you will avoid adding the listener multiple times.

2- Data from the listener should be saved to variables
Thunkable does not allow accessing the components of one screen from another screen. So if you are in screen 2 and the DataChanged block is in screen 1 it will execute when the data is changed in Firebase but it will fail to update labels in screen 1 because you are currently using screen 2.
The remedy to this is to save the information into a variable and when the screen becomes active, you update the labels from the variable that was changed by the DataChanged block.

Hope you find it clear.

1 Like

This topic was automatically closed 90 days after the last reply. New replies are no longer allowed.