0

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
  • 130,605
  • 17
  • 163
  • 193

2 Answers2

0

You don't need to implement another thread. The Listener gives you the data in it's onDataChange method. You can then control the view by setting its visibility.

Alternatively, since the data is only fetched once and that too during app launch, you could get it in a splash screen and then go to your MainActivity.

Edit: As @AbhinavChauhan suggested in the comment section you could also implement this using a progressBar to which would be a type of indicator to show that some data is loading.

Network operations should never be a synchronus task.

Vishist Varugeese
  • 1,500
  • 1
  • 17
  • 30
  • so if you don't show them the main activity then what will you show them, i think you should use the progressbar to show that the something is comming, and hide the progress when the data is loaded – Abhinav Chauhan Mar 20 '20 at 13:31
  • The question is pretty vague and so can be implemented in many ways, but yes I agree with you. Using a progreebar is probably a good approach. – Vishist Varugeese Mar 20 '20 at 13:35
  • Sorry for not being clear asking the question but I was already doing these in a splash screen. The problem was during the launch I have about 5-6 listeners that get data from Firebase. Therefore, I need to have something that will keep the application in slash screen until all those 5-6 `onDataChange` methods finish their jobs. –  Mar 20 '20 at 15:28
0

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:

Alex Mamo
  • 130,605
  • 17
  • 163
  • 193
  • First of all, thanks for your answer and editing my question. I just watched your Youtube video following the link you provide and got what I need there. The problem was during the launch I have about 5-6 listeners that get data from Firebase. I think I wasn't very clear while asking the question. What I wanted my 'thread' to do was waiting for boolean values to be true which are set at the end of `onDataChange()` methods before letting the main activity start. I saw that you used almost the same approach but using `onCallBack()` method which should be better than implementing a thread. –  Mar 20 '20 at 14:13
  • 1
    Good to hear that. Yes, it's almost the same thing using that callback. – Alex Mamo Mar 21 '20 at 11:33