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);
- how pass
outputfile
oncomplete
? - is there way create observable , subscride in 1 event or in less amount of code?
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.you can write code continuously anonymous methods, , if use lambdas cut out more boiler-plate code.
if delete file @
oncompleted
indeed executed @ main thread, specifyobserveron()
.
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
Post a Comment