How to check if a write job in Firebase completed successfully

I am completely new to Firebase and need to know how to check if my writing work was successful, because if I do not, MainActivity launches and MainActivity my Register progress.

This checks to see if Username already running, and logs in the user if it is not:

 Query usernamequery = myRef.orderByChild("Username").equalTo(Username); usernamequery.addListenerForSingleValueEvent(new ValueEventListener() { @Override public void onDataChange(DataSnapshot snapshot) { if (snapshot.exists()) { // TODO: handle the case where the data already exists editText.setError("Username taken!"); return; } else { // TODO: handle the case where the data does not yet exist myRef.child("Users").child(Username).child("Userid").setValue(user.getUid()); myRef.child("Users").child(Username).child("Username").setValue(Username); startActivity(maps); finish(); } } @Override public void onCancelled(DatabaseError databaseError) { Toast.makeText(Username.this, "Error", Toast.LENGTH_LONG).show(); } }); 

But I want the intention for the main activity (maps) to be launched only when myRef.child("Users").child(Username).child("Userid").setValue(user.getUid());

and the other completed with his task and successfully.

What can I do?

+5
source share
1 answer

To find out when recording finished, add a completion listener:

 myRef.child("Users").child(Username).child("Userid").setValue(user.getUid(), new DatabaseReference.CompletionListener() { void onComplete(DatabaseError error, DatabaseReference ref) { System.out.println("Value was set. Error = "+error); } }); 

If you want to write to multiple locations with one operation, you need to look at the update() method: https://firebase.google.com/docs/database/android/read-and-write#updating_or_deleting_data

The following is the correct overload for setValue() : https://firebase.google.com/docs/reference/android/com/google/firebase/database/DatabaseReference.html#setValue(java.lang.Object,%20com.google. firebase.database.DatabaseReference.CompletionListener)

+19
source

Source: https://habr.com/ru/post/1262104/


All Articles