簡體   English   中英

從與 RxJava 的集成改造中獲得實時數據響應

[英]get livedata response from integrated retrofit with RxJava

我想從與 RxJava 集成的 Retrofit 獲得 Livedata 響應以進行調用 API。 我知道 RxJava 響應是流,不能直接將其放入 Livedata。所以感謝您的幫助。

這是一個使用 RxJava 集成 Retrofit 的示例代碼。 如何用 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();
   }
}

你可以這樣做:

  1. 在類中指定MutableLiveData實例
  2. 創建方法來提供它
  3. Observable返回值時將值發布到liveData
  4. 不要忘記處理你的 observables
  5. 畢竟我建議你使用 Repository

您的代碼:

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();
    }

}

您是否考慮過在其中包含一個帶有 LiveData 屬性的存儲庫並從調用中設置其值?

所以一般來說,當你從 Repository getTopContributors調用一個函數時 - 它會返回 liveData,並進行網絡調用。 在網絡訂閱內 - 您更新 liveData

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM