Firebase mobile connection testing issues for android

拜拜、爱过 提交于 2019-12-24 18:36:25

问题


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?


回答1:


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:

  • Firebase database for Android - write events when offline, using persistence
  • Firebase offline no CompletionListener on setValue
  • Firebase database query not returning when offline (on how to detect whether you have a connection)



回答2:


You can use the thread in order to check if a connection is available, since firebase does not provide methods to check that: How to check currently internet connection is available or not in android



来源:https://stackoverflow.com/questions/47600083/firebase-mobile-connection-testing-issues-for-android

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!