1

I've done an Rxjava wrrapper for firebase signInWithCustomToken() method, here is the code:

public Observable<AuthResult> signInWithCustomToken(String token) {
    return Observable.create(new ObservableOnSubscribe<AuthResult>() {
      @Override public void subscribe(ObservableEmitter<AuthResult> emitter) throws Exception {
        firebaseAuth.signInWithCustomToken(token)
            .addOnSuccessListener(new OnSuccessListener<AuthResult>() {
              @Override public void onSuccess(AuthResult result) {
                emitter.onNext(result);
              }
            })
            .addOnFailureListener(new OnFailureListener() {
              @Override public void onFailure(@NonNull Exception e) {
                emitter.onError(e);
              }
            })
            .addOnCompleteListener(new OnCompleteListener<AuthResult>() {
              @Override public void onComplete(@NonNull Task<AuthResult> task) {

                emitter.onComplete();
              }
            });
      }
    });
  }

so I was wondering what is the lifecycle of the three listeners (OnSuccessListener - OnFailureListener() - OnCompleteListener) inside the Rx callback, Do they have the same lifecycle of the return Observable, in other words if I called observable.dispose(), will they be cleared from memory?

and I have another question sorry, is this the best way for modeling such a method in Rx way?

thank you in avance.

Mohamed Ibrahim
  • 3,714
  • 2
  • 22
  • 44

2 Answers2

1

I'm answering my question, calling dispose() doesn't guarantee removing those listeners from memory. but there is a nice solution for this. each time you create an Observable from listeners or callback make sure to setup a Cancellable and clear things there.

emitter.setCancellable(new Cancellable() {
  @Override
  public void cancel() throws Exception {
  //clean memory
 }
});

however the case with code mention in question, that firebase doesn't provide a method to clear those listeners in signwithCustomToken(). but other like DatabaseReference has removeListenr() method to clear things when canceling.

emitter.setCancellable(new Cancellable() {
  @Override
  public void cancel() throws Exception {
    databaseReference.removeEventListener(valueEventListener);
 }
});
Mohamed Ibrahim
  • 3,714
  • 2
  • 22
  • 44
0

Do they have the same lifecycle of the return Observable, in other words if I called observable.dispose(), will they be cleared from memory?

No, you need to specify the disposable logic yourself, and there remove your listeners from firebaseAuth, you can do it using either Emitter.setDisposable() or Emitter.setCancellable(), if you will not supply it, then the Observable will merely untie the connection between the Subscriber and the Observable and will stop emit events, but the listeners will stay registered to firebaseAuth and memory leak might happen.

and I have another question sorry, is this the best way for modeling such a method in Rx way?

using RxJava2 create methods is valid method for wrapping async callbacks methods. However, with RxJava2 the default way with lowest overhead is to extend Observable and use Observer methods to notify events/ register dispose callback. you can read more here.

Community
  • 1
  • 1
yosriz
  • 10,147
  • 2
  • 24
  • 38