Ofek Regev
Ofek Regev

Reputation: 502

RxJava - create a sequence from two Single observable

I have two Singles:

  1. fetching user information data from Facebook.
  2. perform user registration on my server with the information fetched from Facebook.

I want the second Single to start right after the first one called onSuccess, the second Single depends on the data from the first. I'm looking for the right operator to achieve this. any recommendation?

Upvotes: 2

Views: 1461

Answers (2)

taygetos
taygetos

Reputation: 3040

Try flatMap:

fetchUser
    .flatMap(user -> registerUser(user)) // .flatMap(this::registerUser)
    .subscribe()


public Single<User> fetchUser() {...}
public Single<?> registerUser (User user) {...}

Upvotes: 3

Ivan Vovk
Ivan Vovk

Reputation: 1039

Other way to do it with zip

 public Single<List<DataEntry>> getting(String rawSearchText) {
    Single single1 = dbSearch(rawSearchText);
    Single single2 = onlineSearch(rawSearchText);
    return Single.zip(single1, single2, (b1, b2) -> CombineTwoLists(b1, b2))
            .subscribeOn(Schedulers.io())
            .observeOn(AndroidSchedulers.mainThread());
 }

Upvotes: -1

Related Questions