3

I want to display progressDialog while observable is downloading file , and when it's done want to send file to subscriber.

I tried to make my custom subscriber by extends from Subscriber for example:

public abstract class MySubscriber<T> extends Subscriber {
    abstract void onMessage(String message);
    abstract void onDownloaded(File file);
}

and tried to subscribe with it: `

MySubscriber mySubscriber = new MySubscriber() {
            @Override
            public void onMessage(String message) {
                progessDialog.setMessage(message);
            }

            @Override
            public void onCompleted() {

            }

            @Override
            public void onError(Throwable e) {

            }

            @Override
            public void onNext(Object o) {

            }
        };

observable.subscribe(mySubscriber);

observable is :

observable  = Observable.create(new Observable.OnSubscribe<Void>() {
            @Override
            public void call(Subscriber<Void> subscriber) {
                 //file downloading code...
                if (subscriber instanceof MySubscriber){
                ((MySubscriber) subscriber).onMessage("100%");
                ((MySubscriber) subscriber).onDownloaded(file);
               }else{
                   Log.e(TAG,"subscriber is not instance of MySubscriber")
                }
             }

And answer is "subscriber is not instance of MySubscriber"

NickUnuchek
  • 11,794
  • 12
  • 98
  • 138

1 Answers1

3

The reason for subscriber not being of type MySubscriber is because the instance you pass is eventually wrapped by subscribe() in SafeSubscriber:

private static <T> Subscription subscribe(Subscriber<? super T> subscriber, Observable<T> observable) {
    ...
        if(!(subscriber instanceof SafeSubscriber)) {
            subscriber = new SafeSubscriber((Subscriber)subscriber);
        }
    ...
    }
}

If you want to keep using your approach, you can cast subscriber to SafeSubscriber and call SafeSubscriber#getActual() on it to get your instance of MySubscriber.

In your case:

Observable.create(new Observable.OnSubscribe<Void>() {
    @Override
    public void call(Subscriber<? super Void> subscriber) {
        Subscriber yourSubscriber = ((SafeSubscriber) subscriber).getActual();
        ((MySubscriber) yourSubscriber).onMessage("100%");
        ((MySubscriber) yourSubscriber).onDownloaded(file);
    }
});
AndroidEx
  • 15,524
  • 9
  • 54
  • 50
  • I'm using below versions, compile 'io.reactivex:rxandroid:1.2.1' compile 'io.reactivex:rxjava:1.1.6' I'm getting ClassCastException OperationSubscriber cannot cast to LoginSubscriber. public static abstract class LoginSubscriber extends Subscriber { abstract void onUserNameInvalid(String message); } Above solution doesn't works for me. Any solution for this @AndroidEx I was trying to implement below with my custom methods https://github.com/music60/RxAndroid-MVP-Simple/tree/master/app/src/main/java/com/studyjun/rxandroidmvpsimple – Abhijit Kurane May 17 '17 at 10:25
  • Hey @NickUnuchek can help me in this regards. – Abhijit Kurane May 17 '17 at 11:44
  • @AbhijitKurane sorry, I didn't work with the answer above. – NickUnuchek May 17 '17 at 11:51
  • I thought it worked for you as you have accepted the answer by AndroidEx. Thanks for the prompt reply Nick – Abhijit Kurane May 17 '17 at 12:03