cari

Rumah  >  Soal Jawab  >  teks badan

android - Rxjava 的 Repeat 操作符

先看我的代码:(用于测试的)

        People people1 = new People("join","21");
        People people2 = new People("jane","29");

        Observable.just(people1,people2)

                .subscribeOn(AndroidSchedulers.mainThread())    //在主线程上把 UI 初始化
                .doOnNext(people -> {
                    num  = 0;
                    show.setText(String.valueOf(num));
                })

                .repeat(4,Schedulers.newThread())  //在新线程上重复发送2次

                .observeOn(AndroidSchedulers.mainThread())    //在主线程上观测
                .subscribe(peoples1 -> {
                    String s = people1.toString();
                    num++;
                    show.setText(String.valueOf(num));
                    Log.e("---",s);
                });

这里,just先发射第一个 People:people1,之后再发射people2.
但是呢,Logcat如下:

06-29 20:25:20.251 32688-32688/demo.chestnut.com.test E/---: People{name='join', age='21'}
06-29 20:25:20.251 32688-32688/demo.chestnut.com.test E/---: People{name='join', age='21'}
06-29 20:25:20.281 32688-32688/demo.chestnut.com.test E/---: People{name='join', age='21'}
06-29 20:25:20.281 32688-32688/demo.chestnut.com.test E/---: People{name='join', age='21'}
06-29 20:25:20.287 32688-32688/demo.chestnut.com.test E/---: People{name='join', age='21'}
06-29 20:25:20.287 32688-32688/demo.chestnut.com.test E/---: People{name='join', age='21'}
06-29 20:25:20.302 32688-32688/demo.chestnut.com.test E/---: People{name='join', age='21'}
06-29 20:25:20.302 32688-32688/demo.chestnut.com.test E/---: People{name='join', age='21'}

这里打印了8次的 people1,按理由,不是应该打印4次 people1,打印4次的people2吗?

PHPzPHPz2771 hari yang lalu710

membalas semua(1)saya akan balas

  • 伊谢尔伦

    伊谢尔伦2017-04-17 17:23:24

    .subscribe(peoples1 -> {
        String s = people1.toString(); //问题出在这里的`people1` ,本该用`peoples1`的
        num++;
        show.setText(String.valueOf(num));
        Log.e("---",s);
    });

    balas
    0
  • Batalbalas