I have started learning RxAndroid and below is the code I wrote to iterate over a model object (Results) that contains data fetched from the server. I'm iterating over the model object in the observable and providing a newly created object in the observer. I'm trying to take subscription of the observer to unsubscribe the task upon Orientation changes of the fragment. However the subscribe()
returns VOID instead of subscription object.
Questions:
- Does the latest version of RxAndroid handle unsubscription itself upon configuration/orientation change?
In case configuration change happens before the task is complete, the only way to restart this task that I can think of is, I persist the server response in onSavedInstance() and retrieve it from bundle when the fragment is recreated. It'll require booleans to figure out if the configuration change happened before the configuration change or not. Is there a graceful and cleaner way of coping with this?
private void createComicList(final List<Result> marvelResults) { final MarvelComics marvelComics = new MarvelComics(); Observable marvelObservable2 = Observable.create(new ObservableOnSubscribe<MarvelComic>() { @Override public void subscribe(ObservableEmitter<MarvelComic> e) throws Exception { for(Result result : marvelResults) { MarvelComic marvelComic = new MarvelComic(); marvelComic.setDescription(result.getDescription()); marvelComic.setTitle(result.getTitle()); marvelComic.setPageCount(result.getPageCount()); marvelComic.setThumbnailUrl(result.getThumbnail().getPath()); marvelComic.setId(result.getId()); e.onNext(marvelComic); } e.onComplete(); } }); marvelObservable2.subscribeOn(Schedulers.io()) .observeOn(AndroidSchedulers.mainThread()) .subscribe(new Observer<MarvelComic>() { @Override public void onSubscribe(Disposable d) { } @Override public void onNext(MarvelComic comic) { marvelComics.getMarvelComicList().add(comic); } @Override public void onError(Throwable e) { } @Override public void onComplete() { showToast(); } }); }