WhiteFox
WhiteFox

Reputation: 3

RxAndroid how to(is it possible) put some parameters into Observable

My trouble is that I need to put some parameters(String as example) into Observable for some work with it, how can i do it, and is it possible? Pretty simple sample is that how we send params into AsyncTask:

Boolean[] myTaskParams = { true, true, true };
myAsyncTask = new myAsyncTask ().execute(myTaskParams);

Upvotes: 0

Views: 1201

Answers (2)

akarnokd
akarnokd

Reputation: 70017

When you write your async task, either capture the parameters:

Object[] params = { false, true, true};
Observable
.fromCallable(() -> { use params; return value; })
.subscribeOn(Schedulers.io())
.observeOn(AndroidSchedulers.mainThread())
.subscribe(...);

or transform the parameters:

Observable.just(params)
.map(p -> { use p; return value; });
...

or produce many values for a single set of parameters:

Observable.just(params)
.flatMap(p -> { use p, return Observable.something; })
...

Upvotes: 1

adnbsr
adnbsr

Reputation: 635

Send your params to normal function and return your Obsverable type and let it run in background.

public T yourFunction(A a,B b,C c){
    ...
    return t;
}

Observable.from(yourFunction(a,b,c))
      .subscribeOn(Schedulers.newThread())
      .observeOn(AndroidSchedulers.mainThread())
      .subscribe(yourTObserver);

Upvotes: 0

Related Questions