android - Rxjava 的 Repeat 操作符
問題描述
先看我的代碼:(用于測(cè)試的)
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()) //在新線程上重復(fù)發(fā)送2次.observeOn(AndroidSchedulers.mainThread()) //在主線程上觀測(cè).subscribe(peoples1 -> { String s = people1.toString(); num++; show.setText(String.valueOf(num)); Log.e('---',s);});
這里,just先發(fā)射第一個(gè) People:people1,之后再發(fā)射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,按理由,不是應(yīng)該打印4次 people1,打印4次的people2嗎?
問題解答
回答1:.subscribe(peoples1 -> { String s = people1.toString(); //問題出在這里的`people1` ,本該用`peoples1`的 num++; show.setText(String.valueOf(num)); Log.e('---',s);});
相關(guān)文章:
1. angular.js - angular中的a標(biāo)簽不起作用2. android - Genymotion 微信閃退 not find plugin.location_google.GoogleProxyUI3. python3.x - python連oanda的模擬交易api獲取json問題第五問4. springboot中不能獲取post請(qǐng)求參數(shù)的解決方法5. 我畢業(yè)以后在工作之余學(xué)了 PHP,都是自學(xué) 現(xiàn)在在找這方面的工作 求前輩指導(dǎo)學(xué)習(xí)方向 工作常用的知識(shí)6. sql語句如何按or排序取出記錄7. 數(shù)據(jù)表里沒數(shù)據(jù)顯示8. Mac環(huán)境下QT編譯MySQL驅(qū)動(dòng)屢次失敗?如何?9. php怎么用isMobile()函數(shù)識(shí)別pc端移動(dòng)端自動(dòng)跳轉(zhuǎn)10. 輸入地址報(bào)以下截圖錯(cuò)誤,怎么辦?
