简体   繁体   English

Android改造:使用还是不使用RxAndroid?

[英]Android Retrofit: use or not use RxAndroid?

I've been searching the best way to do API-calls , and for that, I did some research on the main ways, like Retrofit , Volley , Ion etc, and I'm convinced that Retrofit is really the best. 我一直在寻找进行API-calls的最佳方法,为此,我对主要方法进行了一些研究,例如RetrofitVolleyIon等,我坚信Retrofit确实是最好的。 I'm used to use this one since a long time, and I should just move on and live my life, with my mind full of happiness about my past choices, BUT, I only use Retrofit , and its calls treatments, where I usually do my stuff, treating success and error , and it works fine, GG. 我已经使用了很长时间了,我应该继续前进并过着自己的生活,对过去的选择充满信心,但是,我只使用Retrofit及其调用疗法,通常尽我所能,对待successerror ,它很好用,GG。 So, I heard about RxAndroid on Retrofit , and then I got myself in a huge doubt about myself: 因此,我在Retrofit上听说了RxAndroid ,然后我对自己产生了极大的疑问:

Should I implement RxAndroid ? 我应该实现RxAndroid吗?

It's going to make my projects better? 它将使我的项目变得更好吗? What are the benefits of using (or not) it? 使用(或不使用)它有什么好处?

I'm guessing you probably mean RxJava in this context. 我猜您可能是在这种情况下使用RxJava RxJava and Retrofit fit very nicely together....particularly if you need to for example combine execution of different requests. RxJava和Retrofit非常完美地契合在一起。...尤其是如果您需要例如将不同请求的执行合并在一起时。 For example say you have following and you want to get token before calling getData 例如,假设您已关注并想在调用getData之前获取令牌

public interface SomeRetrofitServiceInterface {
    @POST("issueToken")
    Observable<String>  issueToken(@Query("subscriptionKey") String subscriptonKey);

    @GET("getSomeData")
    Observable<MyPojo> getData(@Query("token") String token);
}

this is how you can do this using RxJava 这就是您如何使用RxJava做到这一点

service.issueToken(subscriptionKey)
                .subscribeOn(Schedulers.io())
                .flatMap(token -> service.getData(token));

Another use case might be where you want to run a number of requests in parallel (eg using Observable.zip() and wait for them all to complete before doing further processing. 另一个用例可能是您要并行运行多个请求(例如,使用Observable.zip()并等待它们全部完成,然后再进行进一步处理)。

If you are already familiar with the whole RX-way of thinking, I would say: give it a try. 如果您已经熟悉整个RX思维方式,我会说:尝试一下。 For me, it made things in my app way easier, but the impact is dependent on how your app is setup. 对我来说,这使我的应用程序变得更容易,但其影响取决于应用程序的设置方式。

If you're not familiar with RX, I would suggest you first try to grasp the idea behind it, incorporating it in some small parts in your app, without using it in combination with Retrofit. 如果您不熟悉RX,我建议您首先尝试掌握它背后的想法,将其合并到应用程序的某些小部分中,而不要与Retrofit结合使用。

And last but certainly not least: Don't use it because it's hip and happening, try it out first, if you find it valuable for your code, keep on using it. 最后但并非最不重要的一点:不要使用它,因为它很流行并且正在发生,请首先尝试一下,如果您发现它对您的代码有价值,请继续使用它。

RxJava like each library have same negative affects for performance and size of your application. 像每个库一样,RxJava对应用程序的性能和大小也有相同的负面影响。 In this case it is not a big overhead. 在这种情况下,这不是很大的开销。 RxJava is simple and improves the quality of your code. RxJava很简单,可以提高代码质量。 When you start to use it, you will use it in all of your projects. 当您开始使用它时,您将在所有项目中使用它。

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

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