programadorloco
programadorloco

Reputation: 89

RXJava - concat doesn't works

I've a problem using RXJava and Kotlin.

I'm using LiveData and Rx, my problem is that concat method is not concatenating two arrays that I have in Dummy data.

This is my code:

 disposables.
                add(
                Observable.concat(Observable.just(DummyContent.COMPANIES),
                        Observable.just(DummyContent.COMPANIES))
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe({
                    Timber.d("get companies success ${it.size}" )
                    companiesLiveData.value = it
                },{
                     t -> Timber.e(t, "get companies error")
                }))

DummyContent:

 val COMPANIES: MutableList<Company> = ArrayList()
  ...
   // Add some sample items.
        for (i in 1..4) {
            addCompany(createDummyCompany(i))
        }

When I run the code, I only see 4 elements in the list, I was expecting to receive 4 + 4 = 8 items because the concat method.

In the LogCat I see this message twice: get companies success 4

How I can achieve that?

Upvotes: 2

Views: 1059

Answers (1)

tynn
tynn

Reputation: 39843

The Observable.concat() concatenates the two stream given to it. Thus you'll receive two events with the single list. If you want to merge the lists itself, you'd have to emit the single elements instead of the list and make the elements a list again.

Observable.concat(Observable.fromIterable(DummyContent.COMPANIES),
                  Observable.fromIterable(DummyContent.COMPANIES))
          .toList()

Upvotes: 4

Related Questions