2

我在尝试使用BehaviorSubject(via RxRelay.BehaviorRelay) 来存储来自连续的最新发射时遇到了麻烦Observable

通过“连续”,我的意思是源Observable被设计为在底层数据集发生变化时发出数据。

BehaviorSubject订阅了source Observable

看来,一旦我订阅了BehaviorSubject,我似乎只收到从源发出的第一个值ObservableBehaviorSubject. 源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).
});
4

1 回答 1

1

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

于 2016-11-10T09:14:13.990 回答