没有连接时,不会调用插入/删除文档回调上的Firestore数据库
我正在尝试在Android上使用Firestore数据库.
I am trying firestore database on Android.
这是我插入文档的代码:
This is my code that inserts a document:
public Observable<Post> createPost(final Post post){
return Observable.create(new Observable.OnSubscribe<Post>() {
@Override
public void call(final Subscriber<? super Post> subscriber) {
try{
DocumentReference documentReference = getCollection().document();
post.setId(documentReference.getId());
documentReference.set(post).addOnSuccessListener(new OnSuccessListener<Void>() {
@Override
public void onSuccess(Void aVoid) {
subscriber.onNext(post);
subscriber.onCompleted();
}
}).addOnFailureListener(new OnFailureListener() {
@Override
public void onFailure(@NonNull Exception e) {
subscriber.onError(e);
subscriber.onCompleted();
}
});
}catch (Exception ex){
subscriber.onError(ex);
Log.e(LOG_TAG, ex.getMessage(), ex);
}
}
});
}
文档被插入数据库,但是onSuccess和onFailure回调均未调用.
The document gets inserted into the database but neither of the onSuccess nor the onFailure callbacks are invoked.
-
这个问题有时并不一致,有时一个小时后调用回调,有时3小时后等.
The issue is not consistent sometimes it works, sometimes the callbacks are invoked after an hour, sometimes after 3 hours etc..
当没有互联网连接时就会发生这种情况.
This is happening when there is no internet connection.
更新2
- 在此处报告了该问题,并已将其关闭.我不确定如何保证离线创建的数据的正确性.
- The issue was reported here and it is closed. I am not sure how to guarantee the correctness of data created offline.
我遇到了带有本机响应的问题.
I came across this with react native.
对于插入,关键是创建一个新文档.
For inserts the key is to create a new document.
示例:
const userRef = firebase.firestore()
.collection("users")
.doc();
userRef.set({name: someName});
这将离线创建文档并在您重新联机时进行同步.
This will create the document offline and sync when you come back online.
诸如此类的其他呼叫将在离线状态下工作
Further calls such as this will work offline
userRef.collection("Locations").add({location: "Austin,TX"});