gpt4 book ai didi

java - 如何使用 ReplaySubject 重新运行 CompositeSubscription?

转载 作者:行者123 更新时间:2023-11-29 04:56:22 27 4
gpt4 key购买 nike

我有 CompositeSubscription ,并在那里添加带有 ReplaySubject 的 Subscription

 CompositeSubscription compositeSubscription = new CompositeSubscription();
ReplaySubject subject = ReplaySubject.create();

compositeSubscription.add(
manager.getAllContacts()
.toList()
.doOnNext(new Action1<List<Person>>() {
@Override
public void call(List<Person> persons) {
allPersons = persons;
Log.e(TAG, "BookContacts: " + "allPersons = " + allPersons.size());
setupViewPager();
}
})
.subscribe(subject));

然后我用这个 ReplaySubject 添加第二个订阅

compositeSubscription.add(Observable.combineLatest(subject,
(PublishSubject<List<CustomUser>>) execute(
manager.getDigitsContacts()),
new Func2<List<Person>, List<CustomUser>, Object>() {
@Override
public Object call(List<Person> persons, List<CustomUser> customUsers) {
//... my code with persons and customUsers...
return null;
}
})
.subscribe());

代码正常运行,在完成 ReplaySubject hasCompleted = true 之后。

但是当我尝试添加第三个 Subscription 时,它不会调用“call()”方法

compositeSubscription.add(Observable.combineLatest(subject,
(PublishSubject<List<CustomUser>>) execute(
manager.getFacebookContacts()), //<-----manager.getFacebookContacts() is run, but doesn't call call() method
new Func2<List<Person>, List<CustomUser>, Object>() {
@Override
public Object call(List<Person> persons, List<CustomUser> customUsers) {
//...this method is not called after "manager.getFacebookContacts()"
return null;
}
})
.subscribeOn(Schedulers.newThread())
.subscribe());

如何解决?...因为如果我同时添加订阅,它就可以正常工作。

最佳答案

能否请您将错误回调添加到.subscribe()?我的猜测是第三次 ReplaySubject 溢出了 combineLatest 的缓冲区。您应该使用 .replay().autoConnect(0)

而不是创建主题

CompositeSubscription compositeSubscription = new CompositeSubscription();

Observable<List<Person>> persons = manager.getAllContacts()
.toList()
.doOnNext(new Action1<List<Person>>() {
@Override
public void call(List<Person> persons) {
allPersons = persons;
Log.e(TAG, "BookContacts: " + "allPersons = " + allPersons.size());
setupViewPager();
}
}).replay().autoConnect(0, s -> compositeSubscription.add(s));

然后使用persons代替subject

关于java - 如何使用 ReplaySubject 重新运行 CompositeSubscription?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33611142/

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