java - How to create custom Subscriber? -
i want display progressdialog
while observable
downloading file , , when it's done want send file subscriber.
i tried make custom subscriber extends subscriber
example:
public abstract class mysubscriber<t> extends subscriber { abstract void onmessage(string message); abstract void ondownloaded(file file); }
and tried subscribe 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 :
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 not instance of mysubscriber") } }
and answer "subscriber not instance of mysubscriber"
the reason subscriber
not being of type mysubscriber
because instance pass wrapped 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 want keep using approach, can cast subscriber
safesubscriber
, call safesubscriber#getactual()
on instance of mysubscriber
.
in 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); } });
Comments
Post a Comment