适用于Android的Firebase移动连接测试问题 [英] Firebase mobile connection testing issues for android
问题描述
我正在使用Firebase Realtime Database构建应用程序,并且正在测试onFailure回调.我关闭了移动数据并运行下面的代码.
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();
}
});
问题是吐司消息没有弹出,然后当我重新打开移动数据时,突然,我的firebase数据库在几秒钟后被填充了.现在,我了解到在设置FirebaseDatabase.getInstance().setPersistenceEnabled(true)时应该会发生这种情况,但是我没有此设置,也不想设置它.互联网连接关闭时,该应用应显示祝酒消息,因为它应该无法写入.有人知道为什么会这样吗?
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?
推荐答案
onFailure
回调仅在服务器上写入失败时触发,通常是因为您的安全规则拒绝了该回调.
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.
另请参阅:
- 适用于Android的Firebase数据库-离线时写入事件,使用持久性
- Firebase脱机时,setValue上没有CompletionListener
- 离线状态下不返回Firebase数据库查询(关于如何检测您是否有连接)
- 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)
这篇关于适用于Android的Firebase移动连接测试问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!