Firestore Database On Insert/delete Document Callbacks Not Being Invoked When There Is No Connection
Solution 1:
There does not seem to be anything wrong with your code, try perhaps the onCompleteListener
callback. So add
.addOnCompleteListener((Task<Void> task) -> {
if(task.getException() != null) {
emitter.onError(task.getException());
}
if(task.isComplete()) { //try task.isSuccessful() if this is what you are looking for
emitter.onComplete();
}
});
If this does not solve your issue perhaps, use an emitter like so:
Completable completable$ = Completable.create((CompletableEmitter emitter) -> {
firebaseFirestore.collection(collection).document(document)
.delete()
.addOnSuccessListener((Void aVoid) -> emitter.onComplete())
.addOnFailureListener((Exception e) -> emitter.onError(e))
.addOnCompleteListener((Task<Void> task) -> {
if(task.getException() != null) {
emitter.onError(task.getException());
}
if(task.isComplete()) { //try task.isSuccessful()
emitter.onComplete();
}
});
});
return completable$;
Solution 2:
Okay so I did a simple version of your question but instead of adding a post, it adds a User
. The concept is the same.
Here is the method to add a user. It returns an Observable<DocumentReference>
just to reference where the user was added.
publicObservable<DocumentReference> insertToFirebaseFirestore$() {
UserEntity userEntity = newUserEntity();
userEntity.setEmail("myemail@myemail.com");
userEntity.setBio("I'm a cool cat!");
userEntity.setDisplayName("KoolKat!");
//Notice here I am using an ObservableEmitter instead of Subscriber like you didreturnObservable.create((ObservableEmitter<DocumentReference> emitter) -> {
this.firebaseFirestore.collection("tempUsers")
.add(userEntity)
.addOnFailureListener(newOnFailureListener() {
@OverridepublicvoidonFailure(@NonNull Exception e) {
emitter.onError(e);
}
})
.addOnSuccessListener(newOnSuccessListener<DocumentReference>() {
@OverridepublicvoidonSuccess(DocumentReference documentReference) {
//this gets triggered when I run
emitter.onNext(documentReference);
}
})
.addOnCompleteListener(newOnCompleteListener<DocumentReference>() {
@OverridepublicvoidonComplete(@NonNull Task<DocumentReference> task) {
//this also gets triggered when I run
emitter.onNext(task.getResult());
}
});
});
}
When I run this, and place breakpoints inside onSuccess
and onComplete
. Both of them are triggered and I can see the output.
I call the method from the Activity as follows.
...onCreate method
insertToFirebaseFirestore$()
.subscribeOn(Schedulers.io())
.observeOn(Schedulers.io()) //observe on io thread cause I don't need it to updateUI..subscribe((DocumentReference val) ->{
Log.e("USERACTIVITY", "You have uploaded " + val.getId());
});
The LogcatPrints
12-13 09:47:47.942 15007-15059/com.example.debug E/USERACTIVITY: You have uploaded sFBsF4ZmwGaDdxCEKuF6
12-13 09:47:57.563 15007-15059/com.example.debug E/USERACTIVITY: You have uploaded sFBsF4ZmwGaDdxCEKuF6.
- From what I have see with yours, perhaps use an
emitter
within yourObservable.create
. - If that doesn't work try doing the firestore call without wrapping it in an observable
- If all else, might be a connection issue, since you say it happens intermittently
Solution 3:
I came across this with react native.
For inserts the key is to create a new document.
example:
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"});
Post a Comment for "Firestore Database On Insert/delete Document Callbacks Not Being Invoked When There Is No Connection"