RxJava:BehaviorSubject没有从源Observable接收后续排放

时间:2016-11-08 22:37:14

标签: android rx-java reactivex behaviorsubject

我在尝试使用BehaviorSubject(通过RxRelay.BehaviorRelay)存储来自连续Observable的最新发射时遇到问题。

“连续”,我的意思是源Observable用于在基础数据集发生变化时发出数据。

BehaviorSubject订阅了来源Observable

看来,一旦我订阅BehaviorSubject,我似乎只会收到从源Observable发送到BehaviorSubject的第一个值。源Observable似乎不再连续发射,事实上,不再发射任何物品。

所以,这是一个有点人为的例子:

//A Singleton
public class DataManager {

    private Observable<List<Item>> itemsObservable;

    //A BehaviorRelay (BehaviorSubject)
    public BehaviorRelay<List<Item>> itemsRelay = BehaviorRelay.create();

    private DataManager() {

        //An Observable which emits when subscribed, and then subsequently when the underlying uri's data changes
        itemsObservable = SqlBrite.createQuery(Uri uri, ...);

        //In practice I would lazily subscribe to the relay.
        itemsObservable.subscribe(itemsRelay);
    }
}

现在,从某处订阅BehaviorSubject

// Subscribe to the BehaviorSubject
DataManager.getInstance.itemsObservable.subscribe(items -> {
    //Here, I would expect 'items' to be the most recent List<Item> emitted from the source Observable to the BehaviorSubject.
    //However, it seems like it's only ever the *first* item emitted from the source Observable to the BehaviorSubject.
    //It seems like the source Observable never emits to the BehaviorSubject more than once, despite the source's underlying 
    //dataset having changed (I am triggering this change in testing).
});

1 个答案:

答案 0 :(得分:1)

事实证明我的测试中只有一个缺陷。没有通知SqlBrite Observable支持更改的uri,因此Observable没有发出任何新值。在Subject生命周期方法中重新订阅的onResume也有一点红色鲱鱼。所有工作都按预期进行。