我正在调用API,并返回自定义对象的列表(Observable<ArrayList<Pin>>
)。在将列表发回给订户之前,我想添加一个正在本地创建的对象,该对象与API调用分开。这是我的代码:
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))
答案 0 :(得分:1)
使用map
操作,您可以通过map
将数据转换为任何内容:
disposable.add(requestInterface.getPins()
.map{ it ->
it.add(customPinObject)
it
}
.observeOn(AndroidSchedulers.mainThread())
.subscribeOn(Schedulers.io())
.subscribe(callback))