Reputation: 12304
I would like to call getNote
in background.
After I get result which is a Note
object and still in background I would like to take two Note's
values - text_encrypted
and date
.
After all I would like to return them to next step and in main thread set values on two textViews.
How could I achive this? Here is my code below.
Observable.fromCallable(() -> NotesDataBase.getNote(id))
.subscribeOn(Schedulers.io())
.observeOn(AndroidSchedulers.mainThread())
.flatMap(notes -> {
new Object[]{
GeneratorAES.decrypt(notes.text_encrypted),
CalendarUtils.showArticleTime(notes.date)
};
})
.subscribe(objects -> {
((TextView) findViewById(R.id.text2)).setText(objects[0]);
((TextView) findViewById(R.id.text1)).setText(objects[1]);
});
I assume I am doing something wrong in first flatMap but I am not sure.
Upvotes: 0
Views: 296
Reputation: 39843
This should not compile since flatMap()
requires you to return an observable from your lambda. Additionally observeOn()
will change the thread for all following operations of the chain.
Instead you have to call observeOn()
after your operations and only map()
the values. To make it nicer, you could also use Pair
instead of Object[]
:
Observable.fromCallable(() -> NotesDataBase.getNote(id))
.subscribeOn(Schedulers.io())
.map(notes -> Pair.create(
GeneratorAES.decrypt(notes.text_encrypted),
CalendarUtils.showArticleTime(notes.date)))
.observeOn(AndroidSchedulers.mainThread())
.subscribe(pair -> {
((TextView) findViewById(R.id.text2)).setText(pair.first());
((TextView) findViewById(R.id.text1)).setText(pair.second());
});
Upvotes: 1