We can execute some code asynchronously in C# Rx, as shown below, using Observable.Start()
. I am wondering what is the equivalent in RxJava.
void Main()
{
AddTwoNumbersAsync (5,4)
.Subscribe(x=>Console.WriteLine(x));
}
IObservable<int> AddTwoNumbersAsync(int a, int b)
{
return Observable.Start(() => AddTwoNumbers(a, b));
}
int AddTwoNumbers(int a, int b)
{
return a + b;
}
You can defer the operation until subscription, and ensure that subscription happens on another thread:
Observable<Integer> sumDeferred = Observable.defer(new Func0<Observable<Integer>>() {
@Override
public Observable<Integer> call() {
return Observable.just(addTwoNumbers(5, 4));
}
}).subscribeOn(Schedulers.io());
sumDeferred.subscribe(...);