如何使用动态数据加载填充Observable

时间:2016-03-22 04:41:40

标签: android rx-java observable

我有一个UI组件,它是从Bus获得的数据中填充的。

subscription.add(dataManager.getRadioModels()
            .subscribeOn(dataManager.getScheduler())
            .observeOn(AndroidSchedulers.mainThread())
            .subscribe(new Subscriber<RadioModel>() {
                @Override public void onCompleted() { }

                @Override public void onError(Throwable e) {
                    Timber.e("There was a problem loading the top stories " + e);
                    e.printStackTrace();
                }

                @Override public void onNext(RadioModel radioModel) {
                    if (radioModel != null){
                        mVersion.setText(radioModel.getPath());
                    }
                }
            }));

问题是在数据管理器中我创建了一个Observable,当找到新项时,我需要将此项添加到Observable。之后订阅无效,因为我订阅了之前的订阅。

@Override
public Observable<RadioModel> getRadioModels() {
    Observable<RadioModel> items = Observable.<RadioModel>empty();

    Observer observer;
    if (observers.containsKey(Radio.class)){
        observer = observers.get(Radio.class);
    }
    else if (isConnected()){
        observer = new Observer(bus, new Class[] { Radio.class });
    }
    else {
        return items;
    }

    Observer.Listener oListener = new Observer.Listener() {
        @Override
        public void objectLost(ProxyBusObject obj) {
            if (obj != null) {
                Log.d(TAG, "objectLost: " + obj.getBusName() + " @ " + obj.getObjPath());
                //items.subscribe(item -> item.remove(obj.getObjPath()));
            }
        }

        @Override
        public void objectDiscovered(ProxyBusObject obj) {
            if (obj != null) {
                Log.d(TAG, "objectDiscovered: " + obj.getBusName() + " @ " + obj.getObjPath());
                items.concatWith(Observable.just(new RadioModel()));
            }
        }
    };

    observer.registerListener(oListener);

    return items;
}

所以基本上是objectDiscovered中的问题,但我不知道解决它的问题。

0 个答案:

没有答案