darkknightsds
darkknightsds

Reputation: 434

How to add item to disposable Observable list using RxAndroid/ Retrofit

I am making a call to an API and returning a list of custom objects (Observable<ArrayList<Pin>>). Before I send the list back to the subscriber, I want to add an object I am creating locally and separately from the API call. Here is my code:

        val requestInterface = Retrofit.Builder()
            .baseUrl(context.resources.getString(R.string.ulr_pins))
            .addConverterFactory(GsonConverterFactory.create())
            .addCallAdapterFactory(RxJava2CallAdapterFactory.create())
            .build().create(PinsService::class.java)

        disposable.add(requestInterface.getPins()
            .observeOn(AndroidSchedulers.mainThread())
            .subscribeOn(Schedulers.io())
            //How do I add a custom Pin object to the list retrieved in requestInterface.getPins before I send it to the callback?
            .subscribe(callback))

Upvotes: 0

Views: 735

Answers (1)

Kevin
Kevin

Reputation: 309

use map operation, you can convert your data to anything by map:

disposable.add(requestInterface.getPins()
        .map{ it ->
            it.add(customPinObject)
            it
        }
        .observeOn(AndroidSchedulers.mainThread())
        .subscribeOn(Schedulers.io())
        .subscribe(callback))

Upvotes: 1

Related Questions