问题描述
我正在使用新的Firebase控制台.我正在尝试将对象保存到数据库,但是没有保存.以下是代码.
I am using the new Firebase console. I am trying to save an object to the database but its not getting saved. Following is the code.
DatabaseReference mFirebaseRef = FirebaseDatabase.getInstance().getReference();
mFirebaseRef.push().setValue(object, new DatabaseReference.CompletionListener() {
@Override
public void onComplete(DatabaseError databaseError, DatabaseReference reference) {
if (databaseError != null) {
Log.e(TAG, "Failed to write message", databaseError.toException());
}
}
});
正确创建了对象".我没有收到任何错误,并且已经在调试中检查了onComplete方法没有被触发.安全规则也适用于写入.
the 'object' is created properly. There is no error I am getting and I have checked in debugging that the onComplete method is not getting triggered.Security Rule is also true for write.
推荐答案
请注意, DatabaseReference.CompletionListener 在数据库服务器已确认操作时触发".运行测试时是否可能没有网络连接?我复制了您的代码,并在定义为object
的手机上成功运行了该代码:
Note that DatabaseReference.CompletionListener fires "when an operation has been acknowledged by the Database servers". Is it possible you did not have a network connection at the time you ran your test? I copied your code and ran it successfully on a phone with object
defined like this:
Object object = new String("Test");
然后,我启用了飞行模式,重新运行了代码,并观察了您描述的行为.在我禁用飞行模式并建立网络连接之前,完成监听器不会启动.
I then enabled airplane mode, re-ran the code and observed the behavior you describe. The completion listener did not fire until I disabled airplane mode and a network connection was established.
此文档.
这篇关于数据未从Android保存到Firebase数据库中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!