【问题标题】:get livedata response from integrated retrofit with RxJava从与 RxJava 的集成改造中获取实时数据响应
【发布时间】:2019-12-20 21:54:11
【问题描述】:

我想从与 RxJava 集成的 Retrofit 获取 Livedata 响应,用于调用 API。 我知道 RxJava 响应是流式的,无法直接放入 Livedata。所以感谢您的帮助。

这是一个使用 RxJava 集成改造的示例代码。 如何用 Livedata 替换 Observable。

class GitHubRxService {

   private GitHubRxApi gitHubApi;

   GitHubRxService() {
       Retrofit retrofit = new Retrofit.Builder()
         .baseUrl("https://api.github.com/")
         .addConverterFactory(GsonConverterFactory.create())
         .addCallAdapterFactory(RxJavaCallAdapterFactory.create())
         .build();

       gitHubApi = retrofit.create(GitHubRxApi.class);
   }

   Observable<String> getTopContributors(String userName) {
       return gitHubApi.listRepos(userName)
         .flatMapIterable(x -> x)
         .flatMap(repo -> gitHubApi.listRepoContributors(userName, repo.getName()))
         .flatMapIterable(x -> x)
         .filter(c -> c.getContributions() > 100)
         .sorted((a, b) -> b.getContributions() - a.getContributions())
         .map(Contributor::getName)
         .distinct();
   }
}

【问题讨论】:

  • 如果您将 Observable 替换为 Single,那么您应该能够将响应发布到 LiveData。

标签: android retrofit rx-java android-livedata


【解决方案1】:

你可以这样做:

  1. 在你的类中指定MutableLiveData instace
  2. 创建方法来提供它
  3. 当您的Observable 返回值时,将值发布到liveData
  4. 别忘了处理你的 observables
  5. 毕竟我建议你使用存储库

您的代码:

class GitHubRxService {

    private GitHubRxApi gitHubApi;
    private MutableLiveData<String> liveData = new MutableLiveData();
    private CompositeDisposable disposables = new CompositeDisposable();
    private GitHubRxService() {
        Retrofit retrofit = new Retrofit.Builder()
                .baseUrl("https://api.github.com/")
                .addConverterFactory(GsonConverterFactory.create())
                .addCallAdapterFactory(RxJavaCallAdapterFactory.create())
                .build();

        gitHubApi = retrofit.create(GitHubRxApi.class);
    }

    public LiveData<String> getLiveData() {
        return liveData;
    }

    void getTopContributors(String userName) {
        Disposable d = gitHubApi.listRepos(userName)
                .flatMapIterable(x -> x)
                .flatMap(repo -> gitHubApi.listRepoContributors(userName, repo.getName()))
                .flatMapIterable(x -> x)
                .filter(c -> c.getContributions() > 100)
                .sorted((a, b) -> b.getContributions() - a.getContributions())
                .map(Contributor::getName)
                .distinct()
                .subscribe(
                        result -> {
                            liveData.postValue(result);
                        },
                        e -> {
                            e.printStackTrace();
                        },
                        () -> {
                            Log.d("Done")
                        }
                );
        disposables.add(d)
    }

    public void dispose(){
        disposables.dispose();
    }

}

【讨论】:

  • 很好的例子! ;) 但是,您为什么建议改用存储库? ViewModel 允许数据在配置更改后保留下来,因此如果我们使用您的解决方案,我们可以两全其美:在配置更改时不断更新和保存的数据。还是我错过了什么?
【解决方案2】:

您是否考虑过拥有一个包含 LiveData 属性的存储库并通过调用设置其值?

所以一般来说,当您从存储库getTopContributors 调用一个函数时,它会返回 liveData,并进行网络调用。在网络订阅内 - 您更新 liveData

【讨论】:

    猜你喜欢
    • 2019-09-28
    • 2017-07-02
    • 1970-01-01
    • 2016-03-23
    • 1970-01-01
    • 2018-06-11
    • 2018-12-29
    • 2020-06-17
    • 2015-11-16
    相关资源
    最近更新 更多