1

so my question says it all. In onCreate I am attempting to query objects, but I want the UI thread to wait until im done querying all my objects so I can use them to create views that my user can see. Does anyone know how to go about doing that with example code?

here is how i am querying.

 Firebase cardReference = mRef.child("aReference");
        cardReference.addValueEventListener(new ValueEventListener() {
            @Override
            public void onDataChange(DataSnapshot dataSnapshot) {
                for (DataSnapshot dataSnapshot1 : dataSnapshot.getChildren()) {
                    CardModel fireBaseCard = dataSnapshot1.getValue(CardModel.class);
                    testCardModelArray.add(fireBaseCard);
                    cardStack.setCardModelIDInArrayList(fireBaseCard.getCardID());
                }
                setUpCardStack();
            }

            @Override
            public void onCancelled(FirebaseError firebaseError) {

            }
        });

Hope you guys can help!

Dr.Android
  • 259
  • 1
  • 8
  • 22
  • 3
    Use a loading screen that dismisses until such time that you finish your query? – AL. Jul 19 '16 at 14:36
  • You definitely do not want to freeze the UI thread. That is UX hell and a surefire way to lose app downloads. Use some sort of loading mechanism (ex- progress bar, loading screen, etc) – mastrgamr Jul 19 '16 at 14:37
  • You better use splash screen(activity). See this example : http://www.androidhive.info/2013/07/how-to-implement-android-splash-screen-2/ – Lubomir Babev Jul 19 '16 at 14:38
  • Trying to make the UI thread wait is a bad idea (see [my answer here](http://stackoverflow.com/questions/33203379/setting-singleton-property-value-in-firebase-listener) for more details). Instead reframe your mind from "first we get the data, then we update the UI" to "when we get the date, we update the UI". A good example of this can be found in the [Firebase quickstart app for the database](https://github.com/firebase/quickstart-android/tree/master/database), but probably also in most other Firebase Android tutorials. – Frank van Puffelen Jul 19 '16 at 15:29
  • @LubomirBabev Splashscreen is generally what i was looking for. – Dr.Android Jul 19 '16 at 15:57
  • @FrankvanPuffelen Yeah i know making the UI wait is a terrible idea. So what does Firebase quickstart do exactly? – Dr.Android Jul 19 '16 at 15:58

2 Answers2

0

Hmm, so you want to run your stuff in a separate background thread?

First, create a private boolean:

private boolean isQueryingFinished = false

Write this in onCreate:

Thread thread = new Thread(new Runnable() {
    @Override
    public void run() {
        // do your stuff here
        isQueryingFinished = true;
    }
});
thread.start();
while(!isQueryingFinished);

This will wait until the thread finished to continue your stuff in onCreate()

You could also do it like that:

while(!isQueryingFinished) {
    try {
        Thread.sleep(500);
    } catch(Exception ex) {/* */}
}

In that way it would check every 0.5 seconds (500 ms) if it is finished.

But I don't think that that is so good.

So maybe it would be better to use some kind of listener:

public interface QueryFinishedListener {
    void onQueryFinished();
}

Then define it in your activity:

private QueryFinishedListener mQueryFinishedListener;

Now create a new one in onCreate():

mQueryFinishedListener = new QueryFinishedListener() {
    public void onQueryFinished() {
        // do whatever you need to do when finished
    }
};
// do your query stuff here
mQueryFinishedListener.onQueryFinished();

Again, I don't think that this is really useful, because if you are doing something that takes longer, the UI will freeze automatically.

xdevs23
  • 3,824
  • 3
  • 20
  • 33
  • 1
    Firebase already runs its data synchronization off the main UI thread. Putting that in another thread is not going to help. The completion listener is a good idea. But keep in mind that Firebase's `ValueEventListener` is already a completion handler. – Frank van Puffelen Jul 19 '16 at 15:24
0

You should use an AsyncTask. Put all your code inside doInBackground method and in onPostExecute make an animation like a spinner or something. `mAsyncTask = new AsyncTask() {

            @Override
            protected String doInBackground(Void... voids) {
                //put your code here
            }

            @Override
            protected void onPostExecute(String s) {
                //make a spinner here
            }
        };
        mAsyncTask.execute();`
Gorg
  • 23
  • 1
  • 2