将Dagger与Observables,RxJava和改造一起使用

时间:2018-07-29 05:51:44

标签: retrofit2 rx-java2 dagger-2

我有一个简单的代码段,如下所示:

  • 我正在使用Dagger向活动注入改造
  • 由于Dagger已插入OnCreate(),因此运行良好
  • 但是,如果我使用如下所示的Observable来使用RxJava,则由于 调用可观察代码时,不会创建改造客户端, 因为注射发生得较晚
  • 我该如何正确地将Observable与Dagger一起使用

public class MainActivity extends AppCompatActivity {

    @Inject
    Retrofit retrofit;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        ((ThisForThatApplication)getApplication()).getNetComponent().inject(this);
    }


    Observable<CategoryHomeModel[]> categoriesObservable = retrofit.
            .create(Restapi.class)
            .getCategories1(new GetHeaders().prepareHeaders())
            .subscribeOn(Schedulers.newThread())
            .observeOn(AndroidSchedulers.mainThread());


}

1 个答案:

答案 0 :(得分:0)

结果是,这很简单...注意到与匕首:D

private Observable<CategoryHomeModel[]> getCategoriesObservable() {
        Observable<CategoryHomeModel[]> categoriesObservable = retrofit
                .create(Restapi.class)
                .getCategories1(new GetHeaders().prepareHeaders())
                .subscribeOn(Schedulers.newThread())
                .observeOn(AndroidSchedulers.mainThread());
        return categoriesObservable;
    }


    private Observable<WalletBalance> getWalletObservable() {
        Observable<WalletBalance> walletObservable = retrofit
                .create(Restapi.class)
                .getWalletBalance1(new GetHeaders().prepareHeaders())
                .subscribeOn(Schedulers.newThread())
                .observeOn(AndroidSchedulers.mainThread());
        return walletObservable;
    }

    private Observable<Settings> getSettingsObservable() {
        Observable<Settings> settingsObservable = retrofit
                .create(Restapi.class)
                .getSettings1(new GetHeaders().prepareHeaders())
                .subscribeOn(Schedulers.newThread())
                .observeOn(AndroidSchedulers.mainThread());
        return settingsObservable;
    }

    private Observable<SwapSettings> getRatingsObservble() {
        Observable<SwapSettings> ratingsObservable = retrofit
                .create(Restapi.class)
                .rateMySwap1(new GetHeaders().prepareHeaders())
                .subscribeOn(Schedulers.newThread())
                .observeOn(AndroidSchedulers.mainThread());
        return ratingsObservable;

    }

    private void executeApis() {
        List<Observable<?>> observables = Lists.newArrayList(getCategoriesObservable(),
                getWalletObservable(), getSettingsObservable(), getRatingsObservble());
        Observable.merge(observables).toList().subscribe(new Consumer<List<Object>>() {
            @Override
            public void accept(List<Object> objects) throws Exception {
                Timber.d("");
            }

        }, new Consumer<Throwable>() {
            @Override
            public void accept(Throwable throwable) throws Exception {
                Timber.d("");
            }
        });
    }