I'm new to RxJava and I chose to use it because I thought it would be well suited to my use case.
I have some Integer values I want to observe over an infinite period of time. Whenever one of these values changes (i.e. an event) I want all of its observers to be called on another thread.
Because of the long observation time requirement I thought I needed to use the BehaviorSubject class (although initially I thought Observable was all that I needed .. seeing as I just need to 'observe'), and I could use the subscribeOn() method to set a scheduler and hence achieve calling the subscribers on a background thread:
private BehaviorSubject<Integer> rotationPositionSubject = BehaviorSubject.createDefault(getRotorPosition());
rotationPositionSubject.subscribeOn(scheduler);
And I have a rotate() method I used to update the rotationPositionSubject which will be called from the main thread:
@Override
public synchronized int rotate()
{
final int newRotorPosition = super.rotate();
rotationPositionSubject.onNext(newRotorPosition);
return newRotorPosition;
}
However with the above code I found that the subscribers are called on the 'main' thread. Examining the docs for subscribeOn():
Returns:
the source ObservableSource modified so that its subscriptions happen on the specified Scheduler
So my above code won't work as I am not using the returned ObservableSource, but the return object is an Observable which is of no use for my application?
The question is then, how do I observe long-term any object and call subscribers on a background thread with RxJava, or is RxJava the wrong choice?