18956

Firebase mobile connection testing issues for android

Question:

I'm building an app using Firebase Realtime Database and I'm testing out the onFailure callback. I turn off my mobile data and run the code below.

mDatabaseReference.updateChildren(childUpdates) .addOnFailureListener(new OnFailureListener() { @Override public void onFailure(@NonNull Exception e) { Toast.makeText(TeamActivity.this, R.string.toast_message_error_add, Toast.LENGTH_LONG).show(); } });

The issue is that the toast message doesn't pop up, then when I turn back on the mobile data, all of a sudden, my firebase database get populated a few seconds after. Now I understand this should happen when you set FirebaseDatabase.getInstance().setPersistenceEnabled(true), but I don't have this set and don't want it set. The app should show the toast message when the internet connection is off, because it should fail to write. Anyone know why this is happening?

Answer1:

The onFailure callback only triggers when the write is fails on the server, typically because it is rejected by your security rules.

Not having a network connection does not trigger a failure, it merely delays the write to the database server until you're back online again.

Also see:

<ul><li><a href="https://stackoverflow.com/questions/46014436/firebase-database-for-android-write-events-when-offline-using-persistence" rel="nofollow">Firebase database for Android - write events when offline, using persistence</a></li> <li><a href="https://stackoverflow.com/questions/37184941/firebase-offline-no-completionlistener-on-setvalue" rel="nofollow">Firebase offline no CompletionListener on setValue</a></li> <li><a href="https://stackoverflow.com/questions/42591313/firebase-database-query-not-returning-when-offline" rel="nofollow">Firebase database query not returning when offline</a> (on how to detect whether you have a connection)</li> </ul>

Answer2:

You can use the thread in order to check if a connection is available, since firebase does not provide methods to check that: <a href="https://stackoverflow.com/q/5474089/9015853" rel="nofollow">How to check currently internet connection is available or not in android</a>

Recommend

  • Android auth firebase error: Local module descriptor class for com.google.firebase.auth not found
  • Use Lombok @RequiredArgsConstructor without @NonNull
  • Firebase Dynamic Link ShortUrl not working in Android
  • Paging Library invalidating data source not working
  • Retrieve RSS feed and display it in text view
  • broadcast receiver inside activity
  • Opening a floating menu (context menu) in Android?
  • Firebase OnSuccessListener is being called when it should fail
  • Android VideoView Quality is so bad on TV Box using Vitamio Library
  • Android RecyclerView Blank Space
  • Unable to retrieve number before incoming call in marshmallow
  • IllegalStateException “System services not available to Activities before onCreate()”
  • android.app.PendingIntent cannot be accessed ouside the package
  • FCM Data messages are not working properly
  • event.getSource() returns null Accessibility in android
  • how to find common suffix in java by using method
  • Display java JPanel in a JFrame
  • onBackPressed() not being executed
  • iOS: Detect app start via notification press
  • how to adjust image in a panel in Java swing?
  • How to access EntityManager inside Entity class in EJB3
  • how to do an event when i swipe from fragment to the other
  • JFileChooser in front of fullscreen Swing application
  • HTML download movie download link
  • How to draw moving and Running sine wave chart using JFree chart in java?
  • Possible to stop flickering java tooltip in heavyweight mode?
  • sending/ receiving email in Java
  • How to set my toolbar fixed while scrolling android
  • Eraser for UIBezierPath
  • AT Commands to Send SMS not working in Windows 8.1
  • Windows forms listbox.selecteditem displaying “System.Data.DataRowView” instead of actual value
  • Suggestions to manage Login/Logout transitions
  • Exception on Android 4.0 `android.os.StrictMode$AndroidBlockGuardPolicy.onNetwork(StrictMode)`
  • apache spark aggregate function using min value
  • unknown Exception android
  • EntityFramework adding new object to nested object collection
  • Checking variable from a different class in C#
  • Sorting a 2D array using the second column C++
  • failed to connect to specific WiFi in android programmatically
  • How can I use threading to 'tick' a timer to be accessed by other threads?