简体   繁体   English

RxJava和Observable在改造API上的调用

[英]RxJava and Observable on retrofit API call

I am new to RxJava and want to understand the Observable. 我是RxJava的新手,想了解Observable。 So, I am making one API call using retrofit and I am observing changes on the result so my questions are: 因此,我正在使用改造进行一次API调用,并且正在观察结果的变化,因此我的问题是:

  1. Does it make a continues API call to fetch the latest result from the server? 是否进行连续的API调用以从服务器获取最新结果?
  2. If yes then how frequently it is calling? 如果是,那么它多久打一次电话?
  3. Is it call once then what's the use to observe the data? 调用一次,然后观察数据有什么用?
  4. Is it call once then we should use RxJava just to save a couple of lines in our code? 调用一次,我们应该只使用RxJava在代码中保存几行吗? We can simply use live data in this case. 在这种情况下,我们可以简单地使用实时数据。
  5. Does it call once per lifecycle of the activity ie(if activity gets killed and open again then it will fetch the new result?) 它是否在活动的每个生命周期中调用一次,即(如果活动被杀死并再次打开,则它将获取新结果?)

Here is a code snippet: 这是一个代码片段:

// fetching list of recipes
 mRecipeListViewModel.getRecipes().observe(this, new Observer<List<Recipe>>() {
            @Override
            public void onChanged(@Nullable List<Recipe> recipes) {
                // showing list of recipes
            }
        });

that's... a lot of questions. 那是...很多问题。

  1. The answer is "it depends". 答案是“取决于”。 Show your service class (where you define the Retrofit call). 显示您的服务类别(在其中定义Retrofit调用)。 Retrofit calls are usually Single instead of Observable because the API will return the result and end the call, so using observable makes no sense in that scenario (Single is... just ONE result or Error). 改造调用通常是Single而不是Observable因为API将返回结果并结束调用,因此在这种情况下使用observable没有意义(单是...一个结果或一个错误)。

  2. Once :) 一次:)

  3. You Observe a an Observable when you're expecting said observable to emit values (via the onNext) so you could Observe a list of things and get its items emitted one by one (for example). 当您希望观察到的可观察对象发出值时(通过onNext),您可以观察一个可观察对象,这样您就可以观察事物列表并逐一发出它的项。 Or you could have an API that is an open socket emitting items every now and then; 或者您可能有一个API,它是一个开放的套接字,不时地发出项目; RXJava can help you subscribe to said emission and deal with each item (onNext()..) RXJava可以帮助您订阅所述发射并处理每个项目(onNext()..)

  4. I'd use LiveData if you have no use for RxJava; 如果您不使用RxJava,我会使用LiveData。 RX is nice, but it's huge and if all yopu're doing is just calling retrofit Single apis, I'd personally not bother with RX. RX很不错,但是它非常庞大,如果您要做的只是调用翻新的Single api,我个人就不会为RX所困扰。 RX's power comes from the multitude (and sometimes impossible to understand or use) operators (map, flatmap, switch, etc). RX的力量来自众多(有时是无法理解或使用)操作员(地图,平面图,开关等)。 and its super fluid API. 及其超级流畅的API。 It can do very powerful things and there's many adapters and extensions written for it. 它可以执行非常强大的功能,并且为此编写了许多适配器和扩展。 It can be misused to hell back and forth too. 它也可以被滥用来回地狱。 It's a moderately advanced tool for very good purposes that I wouldn't use today unless I have a specific use case that Rx Is really good at . 除非我有Rx 真正擅长的特定用例,否则它是一个非常高级的工具,目的非常好,我今天不会使用。

  5. This has nothing to do with RX and more to do with your architecture. 这与RX无关,与架构无关。 Are you calling retrofit in your lifecycle methods? 您是否在生命周期方法中称呼改造? (like onStart?) or are you calling it when the user interacts (press a button for example). (例如onStart?)还是在用户互动时调用它(例如,按一个按钮)。 In either case, RX has nothing to do with either and won't call anything unless told to. 不管哪种情况,RX都与它无关,除非被告知,否则不会调用任何东西。

More Info 更多信息

  • If you have an API that returns a list of " Things " like List<Thing> , you would think to make it Observable<Thing> getThings() in your service class. 如果您有一个返回“ Things ”列表(如List<Thing>的API,则可以考虑在服务类Observable<Thing> getThings() That will work. 那可行。 But that's not how it would work; 但这不是它的工作方式。 the API sends an Array probably (that's why it's a list), so in reality, it must look like Observable<List<Thing>> getThings() , so then when you subscribe, it's gonna look like (this is pseudo code): API可能会发送一个Array(这就是它成为列表的原因),因此实际上,它必须看起来像Observable<List<Thing>> getThings() ,所以当您订阅时,它看起来像(这是伪代码):
api.getThing()
    .blah()
    .blah()
    .subscribe(...)

A subscription will have 3 methods: 订阅将有3种方法:

onError onNext and onComplete onError onNextonComplete

If you look at the OnNext it would look like onNext(List<Thing> things) so, every emission from the Observable, is a LIST, instead of what you'd initially think: " onNext(Thing thing) so you can add things to the list. 如果您查看OnNext ,它将看起来像onNext(List<Thing> things)因此,来自Observable的每个发射都是一个LIST,而不是您最初认为的那样:“ onNext(Thing thing)因此您可以添加things到列表。

That's not how it works. 那不是它的工作原理。 If your API returns a list of Things... you will probably want to declare it as: 如果您的API返回事物列表...您可能希望将其声明为:

Single<List<Thing>> because your subscription will essentially have 2 methods instead of 3: Single<List<Thing>>因为您的订阅实际上将具有2种方法,而不是3种:

onError and onSuccess(List<Thing> things) (apologies if the names aren't 100% accurate, I haven't touched RX in a couple of years). onErroronSuccess(List<Thing> things) (很抱歉,如果名称不是100%准确,那么两年来我都没有碰过RX)。 So you see, the API either returns the list or an error, there's no null, no next, no complete; 如此看来,API要么返回列表,要么返回错误,不存在null,next,complete。 once you get the list, your subscription will never emit again . 一旦获得列表,您的订阅将不再发出

I hope this helps you a little bit more to grasp what it means to use RxJava (it's a commitment!) 我希望这可以帮助您更多地了解使用RxJava的含义(这是一个承诺!)

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

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