java RxJava + Retrofit - 进行多次调用

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/30575160/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-11-02 17:19:54  来源:igfitidea点击:

RxJava + Retrofit - make multiple calls

javaandroidretrofit

提问by Arek Biela

I have a solid grasp of Retrofit when using sync and async calls. However, I encountered a small problem when creating some complex task, which I have to:

在使用同步和异步调用时,我对 Retrofit 有很好的掌握。但是,我在创建一些复杂的任务时遇到了一个小问题,我必须:

  1. Make a request in order to get List of ID's(about 20-30 ID's)
  2. After fetching ID's list, I would like to make async calls in order to get information about each object, defined by ID. I want to make 20-30 request in pararrel. I desire to observe it in order to update the UI after I manage to receive all the data from async calls.
  1. 发出请求以获取 ID 列表(大约 20-30 个 ID)
  2. 获取 ID 的列表后,我想进行异步调用以获取有关每个对象的信息,由 ID 定义。我想同时提出 20-30 个请求。我希望在我设法从异步调用接收所有数据后观察它以更新 UI。

I read about that issue and I realized that RxJava would solve my problem. But frankly, I have found it really hard so far to understand the whole process.

我读到了这个问题,我意识到 RxJava 可以解决我的问题。但坦率地说,到目前为止,我发现很难理解整个过程。

It would be great if I read some correct example in order to immerse into RxJava/RxAndroid issue.

如果我阅读一些正确的示例以便深入研究 RxJava/RxAndroid 问题,那就太好了。

回答by ffleandro

With this example you can get a list of ids, divide it in individual observables, call a service for each individual id and get notified ntimes for each getDetailsresponse.

通过这个例子,你可以获得一个 id 列表,将它划分为单独的 observables,为每个单独的 id 调用一个服务,并获得n每个getDetails响应的通知时间。

service.getIds()
        .flatMap(ids -> Observable.from(ids))
        .map(id -> service.getDetails(id))
        .subscribe(detailed -> updateUi(detailed));

You can use the Observable.zipfunction to wait on several parallel calls, but I don't know if you can use it in a variable size call.

您可以使用该Observable.zip函数来等待多个并行调用,但我不知道您是否可以在可变大小的调用中使用它。

Take a look at this example:

看看这个例子

Retrofit support for Observable also makes it easy to combine multiple REST calls together. For example, suppose we have one call that gets the photo and a second that gets the metadata. We can zip the results together:

对 Observable 的改造支持还使得将多个 REST 调用组合在一起变得容易。例如,假设我们有一个获取照片的调用和一个获取元数据的调用。我们可以将结果压缩在一起:

Observable.zip(
    service.getUserPhoto(id),
    service.getPhotoMetadata(id),
    (photo, metadata) -> createPhotoWithData(photo, metadata))
    .subscribe(photoWithData -> showPhoto(photoWithData));

回答by memoizr

It seems like what you're really looking for is info or examples on how to get started with RxJava, so I'd suggest you have a look at this excellent series of articles by Dan Lew: http://blog.danlew.net/2014/09/15/grokking-rxjava-part-1/

看起来您真正要寻找的是有关如何开始使用 RxJava 的信息或示例,因此我建议您查看 Dan Lew 撰写的这一系列出色的文章:http: //blog.danlew.net /2014/09/15/grokking-rxjava-part-1/

This series should contain enough material to give you a good idea of how to implement your feature.

本系列应该包含足够的材料,让您对如何实现您的功能有一个很好的了解。