Questions tagged [rx-java]

RxJava – Reactive Extensions for the JVM – a library for composing asynchronous and event-based programs using observable sequences for the Java VM.

RxJava is a Java implementation of Reactive Extensions. Android-specific support for RxJava is provided by .

The main goals of RxJava

  • Stay true to the principles and concepts behind the original implementation while adjusting to the naming conventions and idioms of Java
  • All contracts of Rx should be the same target - the JVM
  • JVM-based languages supported (beyond Java itself) include , , , and
  • Support Java 6+

The library is open sourced under Apache 2.0 license.

References

6958 questions
92
votes
4 answers

What is the difference between an Observer and a Subscriber?

I am trying to decipher the following function: Subscription getCar(id, Observer observer) { return getCarDetails(id, new Observer { @Override onNext(CarDetails details)…
MarcusH
  • 1,693
  • 2
  • 15
  • 20
91
votes
9 answers

In RxJava, how to pass a variable along when chaining observables?

I am chaining async operations using RxJava, and I'd like to pass some variable downstream: Observable .from(modifications) .flatmap( (data1) -> { return op1(data1); }) ... .flatmap( (data2) -> { // How to access data1 here ? …
Julian Go
  • 4,442
  • 3
  • 23
  • 28
85
votes
1 answer

Retrofit with Rxjava Schedulers.newThread() vs Schedulers.io()

What are the benefits to use Schedulers.newThread() vs Schedulers.io() in Retrofit network request. I have seen many examples that use io(), but I want to understand why. Example…
Mikelis Kaneps
  • 4,576
  • 2
  • 34
  • 48
83
votes
5 answers

How to chain two Completable in RxJava2

I have two Completable. I would like to do following scenario: If first Completable gets to onComplete , continue with second Completable. The final results would be onComplete of second Completable. This is how I do it when I have Single…
Mladen Rakonjac
  • 9,562
  • 7
  • 42
  • 55
80
votes
3 answers

How to use CompositeDisposable of RxJava 2?

In RxJava 1, there was CompositeSubscription, but that is not present in RxJava2, There is something CompositeDisposable in rxJava2. How do I use CompositeDisposable or Disposable in RxJava2?
anand gaurav
  • 915
  • 2
  • 7
  • 9
73
votes
10 answers

Android RxJava 2 JUnit test - getMainLooper in android.os.Looper not mocked RuntimeException

I am encountering a RuntimeException when attempting to run JUnit tests for a presenter that is using observeOn(AndroidSchedulers.mainThread()). Since they are pure JUnit tests and not Android instrumentation tests, they don't have access to…
starkej2
  • 11,391
  • 6
  • 35
  • 41
73
votes
17 answers

RxJava delay for each item of list emitted

I'm struggling to implement something I assumed would be fairly simple in Rx. I have a list of items, and I want to have each item emitted with a delay. It seems the Rx delay() operator just shifts the emission of all items by the specified delay,…
athor
  • 6,848
  • 2
  • 34
  • 37
73
votes
3 answers

What is the purpose of doOnNext(...) in RxJava

When should we use doOnNext() from Observable instead of just onNext()?
Aravind Yarram
  • 78,777
  • 46
  • 231
  • 327
72
votes
4 answers

Difference between RxJava API and the Java 9 Flow API

It seems on every iteration of Java for the last few major releases, there are consistently new ways to manage concurrent tasks. In Java 9, we have the Flow API which resembles the Flowable API of RxJava but with Java 9 has a much simpler set of…
Dovmo
  • 8,121
  • 3
  • 30
  • 44
69
votes
9 answers

RxJava: How to convert List of objects to List of another objects

I have the List of SourceObjects and I need to convert it to the List of ResultObjects. I can fetch one object to another using method of ResultObject: convertFromSource(srcObj); of course I can do it like this: public void…
Yura Buyaroff
  • 1,718
  • 3
  • 18
  • 31
66
votes
4 answers

How to create a retrofit.Response object during Unit Testing with Retrofit 2

While using RxJava and Retrofit 2 I am trying to create Unit Tests to cover when my app receives specific responses. The issue I have is that with Retrofit 2 I cannot see a nice way of creating a retrofit.Response object without the use of…
Graham Smith
  • 25,627
  • 10
  • 46
  • 69
66
votes
2 answers

Convert Observable> to a sequence of Observable in RxJava

Given a list of cars (List cars), I can do: Observable.just(cars); //returns an Observable that emits one List Observable.from(cars); //returns an Observable that emits a squence of Car Is there a way I can go from an Observable of a…
Giorgio
  • 13,129
  • 12
  • 48
  • 75
66
votes
7 answers

What is the difference between concatMap and flatMap in RxJava

It seems that these 2 functions are pretty similar. They have same signature (accepting rx.functions.Func1> func), and their marble diagrams look exactly same. Can't paste the pics here, but here's one…
Haspemulator
  • 11,050
  • 9
  • 49
  • 76
62
votes
8 answers

Rxjava Android how to use the Zip operator

I am having a lot of trouble understanding the zip operator in RxJava for my android project. Problem I need to be able to send a network request to upload a video Then i need to send a network request to upload a picture to go with it finally i…
feilong
  • 1,564
  • 3
  • 16
  • 22
59
votes
3 answers

Concat VS Merge operator

I was checking the documentation of RXJava and I notice that concat and merge operators seems do the same. I wrote a couple test to be sure. @Test public void testContact() { Observable.concat(Observable.just("Hello"), …
paul
  • 12,873
  • 23
  • 91
  • 153