gpt4 book ai didi

android - RxJava : BehaviorSubject not receiving subsequent emissions from source Observable

转载 作者:行者123 更新时间:2023-11-30 00:53:51 25 4
gpt4 key购买 nike

我在尝试使用 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).
});

最佳答案

结果证明我的测试存在缺陷。支持 SqlBrite Observable 的 uri 未收到更改通知,因此 Observable 未发出任何新值。在 onResume 生命周期方法中重新订阅 Subject 也有一些误会。所有工作都按预期进行。

关于android - RxJava : BehaviorSubject not receiving subsequent emissions from source Observable,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40497627/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com