Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save puf/4a94a01e3c2510298ee46d0a7f90ab75 to your computer and use it in GitHub Desktop.
Save puf/4a94a01e3c2510298ee46d0a7f90ab75 to your computer and use it in GitHub Desktop.
Detect write errors in Firebase Realtime Database in Android
// 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());
}
}
// 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