简体   繁体   English

如何使用RxAndroid / Retrofit将项目添加到一次性可观察列表

[英]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>> ). 我正在调用API并返回自定义对象的列表( 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. 在将列表发回给订户之前,我想添加一个正在本地创建的对象,该对象与API调用分开。 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))

use map operation, you can convert your data to anything by map : 使用map操作,您可以通过map将数据转换为任何内容:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM