如何使用ReplaySubject重新运行CompositeSubscription?

时间:2015-11-09 14:18:05

标签: java android rx-java rx-android

我有CompositeSubscription,在那里我添加了与ReplaySubject的订阅

 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。

但是当我尝试添加第三个订阅时,它不会调用“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());

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

1 个答案:

答案 0 :(得分:3)

是否可以将错误回调添加到.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