简体   繁体   English

结合使用Rxjava和Retrofit

[英]using Rxjava with Retrofit

I'm using Rxjava with Retrofit. 我正在将Rxjava与Retrofit一起使用。 I know how to use it but I'm still not clear about "what Rxjava exactly does with Retrofit" . 我知道如何使用它,但是我仍然不清楚“ Rxjava对Retrofit到底做了什么”


we use retrofit with any converter(like Gson) and Okhttp library for REST API call, once the response is got, it's of Observable type(if using Rxjava otherwise it'll be of CALL type). 我们对任何转换器(例如Gson)和Okhttp库进行翻新,以进行REST API调用,一旦获得响应,它就是可观察类型的(如果使用Rxjava,否则它将是CALL类型的)。 Now the thing is if we already got the response what exactly Rxjava doing with that response? 现在的问题是,如果我们已经获得了响应,那么Rxjava对该响应到底做了什么?


As far as I've got so far by looking into the codes that, Rxjava firstly takes that Observable response and does some operation using operators on that already gotten response and then secondly it just sends the final response to observer from where one can inflate the data into views. 就目前为止我所研究的代码而言,Rxjava首先获取该Observable 响应 ,然后对已获得的响应使用运算符进行一些操作,然后将其发送最终的响应给观察者,从那里可以将其膨胀数据转化为视图。


  1. Firstly enlighten me if I'm wrong anywhere with my understanding of Rxjava. 首先,如果我对Rxjava的理解不正确,请启发我。
  2. What is the need of doing operations on already gotten data as we've already got the data filtered using API queries using retrofit 已经获得的数据进行操作需要什么,因为我们已经使用改型的API查询对数据进行了过滤
  3. Why do we use Customer interface object in Rxjava? 为什么我们在Rxjava中使用Customer接口对象? and so we use it at place of Observer or onNext() . 所以我们在ObserveronNext()处使用它。
  4. Does Rxjava also help in retrieving purpose or it just operates on already gotten data? Rxjava是否也有助于检索目的,或者仅对已获取的数据进行操作?

RXJava is helping you with threading and converting into your existing reactive code. RXJava正在帮助您进行线程化并将其转换为现有的反应式代码。

It uses Flowable/Observable/Single to fetch your Data (for example if you have many of them in a Stream) it allows you to have backpressure using Flowable. 它使用Flowable / Observable / Single来获取您的数据(例如,如果流中有许多数据),它允许您使用Flowable进行反压。

It also allows you to use the basic Reactive stuff like having the request in a different Thread then your observing. 它还允许您使用基本的Reactive东西,例如将请求放在不同的线程中然后进行观察。

That also means that you can have your Results/Requests as reactive stream and map/convert/... and whatever possible with RxJava. 这也意味着您可以将结果/请求作为反应流和map / convert / ...,以及使用RxJava的任何可能。

You dont have to worry about that its a wrapper about your existing data, since the data are fetched inside an Interceptor and converted to use with RxJava. 您不必担心它是现有数据的包装,因为数据是在Interceptor内部获取并转换为可与RxJava一起使用的。

You wont get any performance issues or have any issues at all since it's well tested by the community. 由于社区已经对其进行了充分的测试,因此您不会遇到任何性能问题或根本没有任何问题。

1) Firstly enlighten me if I'm wrong anywhere with my understanding of Rxjava. 1)如果我对Rxjava的理解在任何地方出错,请首先启发我。

RxJava stands for reactive-developing. RxJava代表反应式开发。 It uses a common "coding-style" which is based on functional programming. 它使用基于函数式编程的常见“编码样式”。

2) What is the need of doing operations on already gotten data as we've already got the data filtered using API queries using retrofit 2)对已经获得的数据进行操作需要什么,因为我们已经使用改型的API查询对数据进行了过滤

You may want to have several operations on just one response. 您可能只希望对一个响应执行多个操作。 Which means that you can convert it to different "behaviours" or just merge/concat/zip/filter again. 这意味着您可以将其转换为其他“行为”,或者再次合并/ concat / zip / filter。 In some cases you are not able to control the api and need to process it later which means that reactive can help you in this task. 在某些情况下,您将无法控制api,需要稍后对其进行处理,这意味着React可帮助您完成此任务。

*3) Why do we use Customer interface object in Rxjava? * 3)为什么我们在Rxjava中使用Customer接口对象? and so we use it at place of Observer or onNext(). 因此我们在Observer或onNext()处使用它。 * *

Since you request data you need to consume it. 由于您需要数据,因此需要使用它。 Its like having a list and you want to modify entries in the list it allows you to do that before you consume (output) it. 就像有一个列表,您想要修改列表中的条目一样,它允许您在使用 (输出)它之前执行此操作。 There's also a consumer if an error occured. 如果发生错误,还会有一个消费者。

4) Does Rxjava also help in retrieving purpose or it just operates on already gotten data? 4)Rxjava是否也有助于检索目的,还是仅对已获取的数据进行操作?

Since it's an Interceptor it converts "existing" data into a reactive stream. 由于它是一个拦截器,因此可以将“现有”数据转换为反应流。 It operates on already gotten data. 它对已经获得的数据进行操作。 At least in this case. 至少在这种情况下。

Example (Kotlin): 示例(科特琳):

    var service = Retrofit.Builder()
            .addCallAdapterFactory(RxJava2CallAdapterFactory.createWithScheduler(Schedulers.newThread()))
            .addCallAdapterFactory(ObserveOnMainCallAdapterFactory(AndroidSchedulers.mainThread()))
            .addConverterFactory(GsonConverterFactory.create(GsonUtil.gson()))
            .client(okHttpClient)
            .baseUrl(host!!)
            .build()
            .create(YourService::class.java)

service.getLotsOfData()
       .flatMapIterable{ it }
       .flatMap{ it.id = id+"whatEverModified" }
       .subscribeOn(Schedulers.io)
       .observeOn(AndroidSchedulers.mainThread())

 interface YourService { @GET("lots/of/data") fun getLotsOfData(): Flowable<CustomModel> }

Example (Java): 范例(Java):

 YourService service = Retrofit.Builder()
            .addConverterFactory(GsonConverterFactory.create(GsonUtil.gson()))
            .client(okHttpClient)
            .baseUrl(host)
            .build()
            .create(YourService.class);

service.getLotsOfData()
       .flatMapIterable(data -> data)
       .flatMap( data -> { data = data +"whatEverModified"; return data; })
       .subscribeOn(Schedulers.io)
       .observeOn(AndroidSchedulers.mainThread());

 interface YourService { @GET("lots/of/data") Flowable<CustomModel> getLotsOfData();  }

This code get your List of data, iteratable through each item, appends "whatEverModified" to each id in your Model and do all the work in the IO Thread. 此代码获取可遍历每个项目的数据列表,将“ whatEverModified”附加到模型中的每个id上,并在IO线程中完成所有工作。 After all data are modified and fetched it returns the data in the Main Thread. 修改并提取所有数据后,它将在主线程中返回数据。 Subscribe = Process data, Observe = Consumed data. 订阅=过程数据,观察=已消耗数据。

For a reason why you should use RxJava with Retrofit, I'll point you to a previous answer of mine . 出于某种原因,您应该将RxJava与Retrofit一起使用,我将向您指出我的先前答案 Setting up Retrofit is an one-time cost (both at design and at runtime), but the benefits of conciseness and composability pay off forever. 设置Retrofit是一项一次性成本(在设计和运行时都需要),但是简洁和可组合性的好处将永远得到回报。

Note that I think you're looking at RxJava at the wrong abstraction level - don't think in terms of Observer-Subscriber-Event; 请注意,我认为您在错误的抽象级别上查看RxJava-不要以Observer-Subscriber-Event的方式考虑; think more at the level of datastream with lazy/repeatable operations. 在具有延迟/可重复操作的数据流级别上考虑更多。

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

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