Last active
January 5, 2021 23:02
-
-
Save puf/4a94a01e3c2510298ee46d0a7f90ab75 to your computer and use it in GitHub Desktop.
Detect write errors in Firebase Realtime Database in Android
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// To detect when a write operation is rejected by your Realtime Database | |
// security rules, add a completion listener to the `setValue()` call and | |
// throw the exception if the task failed. | |
val ref = FirebaseDatabase.getInstance().getReference() | |
ref.push() | |
.setValue(ServerValue.TIMESTAMP) | |
.addOnCompleteListener(new CompletionListener() { | |
@Override | |
public void onComplete(Task<Void> task) { | |
Log.i("firebase", String.valueOf(task.isSuccessful())) | |
if (!task.isSuccessful()) { | |
Log.i("firebase", "Throwing exception") | |
throw new RuntimeException(task.getException()); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// To detect when a write operation is rejected by your Realtime Database | |
// security rules, add a completion listener to the `setValue()` call and | |
// throw the exception if the task failed. | |
val ref = FirebaseDatabase.getInstance().reference | |
ref.push() | |
.setValue(ServerValue.TIMESTAMP) | |
.addOnCompleteListener { task -> | |
Log.i("firebase", task.isSuccessful.toString()) | |
if (!task.isSuccessful) { | |
throw task.exception!! | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment