如何在每次调用Observable.create时运行它

时间:2016-03-11 20:31:21

标签: android rx-java

我是RXJava的新手。 我有一个名为politelyrefresh()的函数,它将两个可观察对象连接在一起,但这两个可观察对象中的函数仅在我第一次调用politeRefresh时运行,我不确定这是否是正确的方法。我想要的是每次在observables中运行这个函数。

public void politelyRefresh() {
    Observable.concat(refreshStoreDataObservable, refreshProjectDataObservable)
            .finallyDo(()-> {
                try {     
                 //someother other long runnning-network requests

                } catch (Exception e) {
                    Log.e(TAG, "politelyRefresh finallyDo Error", e);
                }
            })
            .subscribeOn(Schedulers.io())
            .observeOn(AndroidSchedulers.mainThread())
            .subscribe(reloadUiFromLocalStorageSubscriber);

}

//其他observable几乎相同,但发出另一个请求

Observable<String> refreshStoreDataObservable = Observable.create(new Observable.OnSubscribe<String>() {
    @Override
    public void call(Subscriber<? super String> subscriber) {

  //DOESN'T GET HERE SECOND TIME!

        Store.syncStores(new ListCallback() {
            @Override
            public void syncSuccess() {
                getSyncStateManager().setStoresRefreshed();
                subscriber.onCompleted();
            }

            @Override
            public void syncError() {

                subscriber.onError(new Throwable("SYNC STORES ERROR"));
                getSyncStateManager().setStoresSyncCompleted();
            }
        });

    }
});
Subscriber<String> reloadUiFromLocalStorageSubscriber = new Subscriber<String>() {

    @Override
    public void onCompleted() {
        if (mStoreRefreshLayout != null){
            mStoreRefreshLayout.setRefreshing(false);
        }
    }

    @Override
    public void onError(Throwable e) {
        Log.e(TAG, "reloadUiFromLocalStorageSubscriber: onError", e);
        if (mStoreRefreshLayout != null){
            mStoreRefreshLayout.setRefreshing(false);
        }
    }

    @Override
    public void onNext(String s) {
        Log.d(TAG, "reloadUiFromLocalStorageSubscriber: onNext " + s);
    }
};

2 个答案:

答案 0 :(得分:1)

我认为你正在寻找Observable.defer()。这基本上做的是推迟创建Observable到订阅时。

这是一个简单的例子:

public class Refresher {

    Refresher() {
        politelyRefresh();
        politelyRefresh();
    }

    public void politelyRefresh() {
        Observable.defer(() -> Observable.concat(refreshProjectData(), refreshStoreData()))
                .map(this::processData)
                .subscribe(this::printData);
    }

    private Observable<String> refreshStoreData() {
        System.out.println("StoreData Refreshed");
        return Observable.just("data1","data2","data3");
    }

    private Observable<String> refreshProjectData() {
        System.out.println("ProjectData Refreshed");
        return Observable.just("Project1","Project2", "Project3");
    }

    private String processData(String data) {
        return data + " processed";
    }

    private void printData(String data) {
        System.out.println(data);
    }

}

如果您实例化我们的复习对象,您将获得

StoreData Refreshed
StoreData Refreshed
Project1 processed
Project2 processed
Project3 processed
data1 processed
data2 processed
data3 processed
StoreData Refreshed
StoreData Refreshed
Project1 processed
Project2 processed
Project3 processed
data1 processed
data2 processed
data3 processed

如果你想在不同的线程上运行某些东西,你可以在特定的observable上指定你想要在非ui线程上运行。

因此,例如,您可能希望在后台线程上的politelyRefresh中运行Observable并在UI线程上订阅它。其他Observable的创建也将在后台线程中发生!

答案 1 :(得分:0)

我终于通过将订阅者从类实例移动到.subscribe()函数()内部来实现这一点。我不知道为什么会这样。

Observable.concat(refreshStoreDataObservable, refreshProjectDataObservable)
        .finallyDo(()-> {
            try {     
             //someother other long runnning-network requests

            } catch (Exception e) {
                Log.e(TAG, "politelyRefresh finallyDo Error", e);
            }
        })
        .subscribeOn(Schedulers.io())
        .observeOn(AndroidSchedulers.mainThread())
        .subscribe( new Subscriber<String>() { /*rest of code  */}); //**here