I'm trying to create an empty Observalbe to start creating a hard disk image (a long operation) in an IO stream:

  Observable.empty() .subscribeOn(Schedulers.io()) .subscribe(o -> { try { imageCreater.createImage(creatingImageObject, new File(imageCreateView.getImagePath())); } catch (IOException e) { imageCreateView.onError(e.getMessage()); } }); 

However, nothing happens. If you fix Observable.empty() on Observable.just(1) , everything starts working!


Why is this happening?

    1 answer 1

    If you create an Observable using the Observable method, then such an Observable will only onCompleted an onCompleted event and nothing more, i.e. the onNext event onNext never be triggered and your code signed (in subscribe ) to this event will never be executed.

    Observable.just creates an Observable that will produce a predetermined number of values ​​(in your case, this is one value = 1), and then it will end. Thus, the onNext event to which you subscribe to subscribe will be triggered once and your code will be executed once.

    UPD:

    In the first case (when using Observable.empty), your code should look like this:

     Observable.empty() .subscribeOn(Schedulers.io()) .subscribe( value -> {}, throwable -> {}, () -> { try { imageCreater.createImage(creatingImageObject, new File(imageCreateView.getImagePath())); } catch (IOException e) { imageCreateView.onError(e.getMessage()); } } );