android - RXJava pass a value into onComplete -


i'm using rxjava 2. after logic in subscribe method want remove used file. guess should in oncomplete. can not pass there file object. code: questions:

observer<bitmap> observer = new observer<bitmap>() {     @override     public void onsubscribe(disposable d) {     }      @override     public void onnext(bitmap bitmap) {         if(mimageview.gettag().tostring().equals(tag)) {             mimageview.setimagebitmap(bitmap);         }     }      @override     public void onerror(throwable e) {     }      @override     public void oncomplete() {         //            outputfile.delete();     } };  observable<bitmap> observable = observable.create(new observableonsubscribe<bitmap>() {     @override     public void subscribe(observableemitter<bitmap> e) throws exception {         file outputfile = new file(newtag);         // stuff file         bitmap bitmap = decodesampledbitmapfromurl(newtag, width, height);         e.onnext(bitmap);         // need delete file         e.oncomplete();         }     } ); observable.subscribeon(schedulers.newthread()).observeon(androidschedulers.mainthread()).subscribe(observer); 
  1. how pass outputfile oncomplete?
  2. is there way create observable , subscride in 1 event or in less amount of code?

  1. you can't pass object oncomplete(), oncomplete() doesn't have values, signal the streams ends, , no more items emitted.
    can pass @ onnext() each emission (i.e. wrap 2 objects tuple @akarnokd suggested) , delete there.

  2. you can write code continuously anonymous methods, , if use lambdas cut out more boiler-plate code.

  3. if delete file @ oncompleted indeed executed @ main thread, specify observeron().

anyhow, don't have on oncompleted() event, delete on subscription code (create method) after you've extracted bitmap memory, subscriber shouldn't know or care how bitmap extracted. can use fromcallable(),meaning wrap method operations. sum up:

 observable.fromcallable(() -> {         file outputfile = new file(newtag);         // stuff file         bitmap bitmap = decodesampledbitmapfromurl(newtag, width, height);         // sure handle exceptions ensure file closed         outputfile.delete();         return bitmap;     })             .subscribeon(schedulers.newthread())             .observeon(androidschedulers.mainthread())             .subscribe(bitmap -> {                         if (mimageview.gettag().tostring().equals(tag)) {                             mimageview.setimagebitmap(bitmap);                         }                     },                     throwable -> {                         //handle error                     }); 

Comments

Popular posts from this blog

Command prompt result in label. Python 2.7 -

javascript - How do I use URL parameters to change link href on page? -

amazon web services - AWS Route53 Trying To Get Site To Resolve To www -