rx-java2

Subscribewith Vs subscribe in RxJava2(Android)?

倖福魔咒の 提交于 2019-12-03 01:38:10
问题 When to call the subscribeWith method rather than plain subscribe? And what is the use case? compositeDisposable.add(get() .observeOn(AndroidSchedulers.mainThread()) .subscribeOn(Schedulers.io()) .subscribe(this::handleResponse, this::handleError)); VS compositeDisposable.add(get() .observeOn(AndroidSchedulers.mainThread()) .subscribeOn(Schedulers.io()) // .subscribe(this::handleResponse, this::handleError); .subscribeWith(new DisposableObserver<News>() { @Override public void onNext(News

NetworkOnMainThreadException using OkHttp with RxJava2

最后都变了- 提交于 2019-12-02 23:11:35
问题 I am trying to learn Rxjava2. I am facing a problem i.e, NetworkOnMainThreadException during network call using okHttp. I have to use only okHttp libary. This is my method where I have written the code of RxJava2 for calling Login API. @Override public void onServerLoginClick(LoginRequest.ServerLoginRequest serverLoginRequest) { HttpParamObject httpParamObject = ApiGenerator.onServerLogin(serverLoginRequest); Service service = ServiceFactory.getInstance(activity, AppConstants.TASKCODES.LOGIN)

Do I need to free/release Observable object after I use it?

99封情书 提交于 2019-12-02 21:24:07
问题 Do I need to free/release Observable object after I use it in case Observable object is not garbaged by GC? I didn't find any article to mention it. Is Observable object garbaged by GC automatically? Observable.create(new ObservableOnSubscribe<Boolean>() { @Override public void subscribe(ObservableEmitter<Boolean> e) throws Exception { boolean connected = true; //my logic code here e.onNext(connected); e.onComplete(); e.setCancellable(null); //Do I need to do some extra release/free

Rxandroid What's the difference between SubscribeOn and ObserveOn

白昼怎懂夜的黑 提交于 2019-12-02 20:08:08
I am just learning Rx-java and Rxandroid2 and I am just confused what is the major difference between in SubscribeOn and ObserveOn. SubscribeOn specify the Scheduler on which an Observable will operate. ObserveOn specify the Scheduler on which an observer will observe this Observable. So basically SubscribeOn is mostly subscribed (executed) on a background thread ( you do not want to block the UI thread while waiting for the observable) and also in ObserveOn you want to observe the result on a main thread... If you are familiar with AsyncTask then SubscribeOn is similar to doInBackground

Unable to create call adapter for io.reactivex.Observable

夙愿已清 提交于 2019-12-02 19:52:59
I'm going to send a simple get method to my server(it is Rails app) and get the result using RxJava and Retrofit. The thing that I did is: My interface: public interface ApiCall { String SERVICE_ENDPOINT = "https://198.50.214.15"; @GET("/api/post") io.reactivex.Observable<Post> getPost(); } My model is this: public class Post { @SerializedName("id") private String id; @SerializedName("body") private String body; @SerializedName("title") private String title; public String getId () { return id; } public String getBody () { return body; } public String getTitle () { return title; } } And this is

Convert RxJava Observables To Live Data With Kotlin Extension Functions

偶尔善良 提交于 2019-12-02 19:16:15
I've been using alot of RxJava Observables converted to LiveData in my code using LiveDataReactiveStreams.fromPublisher() library. So I though of adding an extension function to the RxJava Observable to easily convert them to LiveData . These are my extension functions: fun <T> Flowable<T>.toLiveData() : LiveData<T> { return LiveDataReactiveStreams.fromPublisher(this) } fun <T> Observable<T>.toLiveData(backPressureStrategy: BackpressureStrategy) : LiveData<T> { return LiveDataReactiveStreams.fromPublisher(this.toFlowable(backPressureStrategy)) } fun <T> Single<T>.toLiveData() : LiveData<T> {

Subscribewith Vs subscribe in RxJava2(Android)?

a 夏天 提交于 2019-12-02 15:29:42
When to call the subscribeWith method rather than plain subscribe? And what is the use case? compositeDisposable.add(get() .observeOn(AndroidSchedulers.mainThread()) .subscribeOn(Schedulers.io()) .subscribe(this::handleResponse, this::handleError)); VS compositeDisposable.add(get() .observeOn(AndroidSchedulers.mainThread()) .subscribeOn(Schedulers.io()) // .subscribe(this::handleResponse, this::handleError); .subscribeWith(new DisposableObserver<News>() { @Override public void onNext(News value) { handleResponse(value); } @Override public void onError(Throwable e) { handleError(e); } @Override

delay and interval operators do not work properly

孤街醉人 提交于 2019-12-02 14:14:43
问题 As shown below, I am creating Observables . I would like to wait specific amount of time in seconds as shown in the code. therefore I used either delay or interval operator. I expected the code to wait i.e. 5 seconds then System.out.println from the observer to be printed. but what happens is, doOnNext is executed and the code never goes further. I mean the execution stops at doOnNext even after the 5 seconds elapsed. Code : public static void main(String[] args) { Observable<List<Person>>

How to subscribe several subscribers to Observable or Flowable?

白昼怎懂夜的黑 提交于 2019-12-02 14:05:04
问题 In Hello World example there is one subscriber public static void main(String[] args) { Flowable.just("Hello world").subscribe(System.out::println); } How to make two or more? 回答1: You can subscribe multiple subsctibers to any observable/flowable. Just repeat subscribe call as many times as you need. Flowable<String> source = Flowable.just("Hello world"); source.subscribe(System.out::println); source.subscribe(System.out::println); ... There is difference in hot and cold observables in the

Handling exceptions inside Observable.fromCallable() when subscription gets cleared

十年热恋 提交于 2019-12-02 11:54:56
I have a situation where a long running process is wrapped in an Observable.fromCallable() . This process is an OkHttp call and, if terminated, will throw an IOException . If the observable is subscribed to, then the disposable is stored in a CompositeDisposable and the exception is handled as expected. However, my code will clear the CompositeDisposable in some cases, triggering the OkHttp thread termination with no error handling, causing the app to crash with an unhandled exception. Here's a simple unit test example of this problem: @Test public void test(){ CompositeDisposable