Getting data from firebase in a synchronized way

bugra.anadol :

I have been developing an Android application where every time the user opens the app, it loads some data from the Firebase Realtime database using addListenerForSingleValueEvent and the app never reads anything else from firebase during the rest of the session.

Everything works well but since this is an asynchronous process, I want to prevent users from entering the main activity before everything is loaded from firebase completely because that may cause views to be loaded before the data they use has arrived.

I thought of a solution: Implementing a thread that will check if all the data is loaded and will only let the main activity start when the process is over.

So my questions are:

1- Would the solution I thought of be a good one?

2- Should I instead use some other approach like using Firebase Rest API or anything else?

Alex Mamo :

I want to prevent users from entering the main activity before everything is loaded from firebase completely

In that case, you need to implement a splash page. So you load the data in an asynchronous way and redirect the user to the MainActivity only when the callback is complete.

Implementing a thread that will check if all the data is loaded and will only let main activity to start when the process is over.

There is no need for any other thread. The onDataChange() will fire only when we getting data from the database is complete.

Would the solution I thought of be a good one?

No. The reason is the above.

Should I instead use some other approach like using Firebase Rest Api or anything else?

You should use the Rest API only if needed. But this will not solve the asynchronous manner of this API.

Getting data from firebase in a synchronized way

Definitely NO! You should handle the APIs asynchronously as intended. For more details, you can check my answer from the following post:

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=334147&siteId=1