Olympus
Olympus

Reputation: 105

Check if firebase has finished adding items to database

I am sending data from my android app to a realtime firebase database.

I would like to check when firebase has finished adding these items into the database, as I will be calling a script on the server that will access the data in the database (through an asynctask).

But before I call the script, I need to make sure that firebase has finished adding all the items as to avoid any conflicts in the data. The code would be something like this:

myRef.child(userID).setValue(items); //add items to firebase database

new SendPostRequest().execute(); //call script on the server that will connect 
                                    to firebase database and retrieve data. 

I was wondering if the script would be called whilst firebase is adding data to the database therefore it would retrieve partial data, so I need to make sure that firebase has finished adding the items into the database first.

Upvotes: 0

Views: 475

Answers (1)

Frank van Puffelen
Frank van Puffelen

Reputation: 598728

You can add a completion listener to setValue() to get notified when the call has completed.

See the section add a completion callback in the Firebase documentation for a full example.

mDatabase.child("users").child(userId).setValue(user)
    .addOnSuccessListener(new OnSuccessListener<Void>() {
        @Override
        public void onSuccess(Void aVoid) {
            // Write was successful!
            // ...
        }
    })
    ...

Upvotes: 2

Related Questions