RxJava/RxAndroid : buffer(long timespan, TimeUnit unit)
以timespan个unit时间单位作为缓冲周期,一轮一轮的把数据流中的数据分批发射出去。
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import java.util.List;
import java.util.concurrent.TimeUnit;
import io.reactivex.Observable;
import io.reactivex.Observer;
import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.disposables.Disposable;
import io.reactivex.schedulers.Schedulers;
public class MainActivity extends AppCompatActivity {
private String TAG = "输出";
private Observer<List<Integer>> mObserver = new Observer<List<Integer>>() {
@Override
public void onSubscribe(Disposable d) {
}
@Override
public void onNext(List<Integer> list) {
Log.d(TAG, "onNext");
for (int i = 0; i < list.size(); i++) {
Log.d(TAG, "onNext:" + list.get(i));
}
}
@Override
public void onComplete() {
Log.d(TAG, "onComplete");
}
@Override
public void onError(Throwable e) {
Log.e(TAG, e.toString(), e);
}
};
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Integer[] numbers = new Integer[100];
for (int i = 0; i < numbers.length; i++) {
numbers[i] = i;
}
Observable.fromArray(numbers)
.buffer(1, TimeUnit.MILLISECONDS)
.subscribeOn(Schedulers.io())
.observeOn(AndroidSchedulers.mainThread())
.subscribeWith(mObserver);
}
}
输出:
01-19 17:12:44.081 21903-21903/zhangphil.book D/输出: onNext
01-19 17:12:44.081 21903-21903/zhangphil.book D/输出: onNext:0
01-19 17:12:44.081 21903-21903/zhangphil.book D/输出: onNext:1
01-19 17:12:44.081 21903-21903/zhangphil.book D/输出: onNext:2
01-19 17:12:44.081 21903-21903/zhangphil.book D/输出: onNext:3
01-19 17:12:44.081 21903-21903/zhangphil.book D/输出: onNext:4
01-19 17:12:44.081 21903-21903/zhangphil.book D/输出: onNext:5
01-19 17:12:44.081 21903-21903/zhangphil.book D/输出: onNext:6
01-19 17:12:44.081 21903-21903/zhangphil.book D/输出: onNext:7
01-19 17:12:44.081 21903-21903/zhangphil.book D/输出: onNext:8
01-19 17:12:44.081 21903-21903/zhangphil.book D/输出: onNext:9
01-19 17:12:44.081 21903-21903/zhangphil.book D/输出: onNext:10
01-19 17:12:44.081 21903-21903/zhangphil.book D/输出: onNext:11
01-19 17:12:44.081 21903-21903/zhangphil.book D/输出: onNext:12
01-19 17:12:44.081 21903-21903/zhangphil.book D/输出: onNext:13
01-19 17:12:44.081 21903-21903/zhangphil.book D/输出: onNext:14
01-19 17:12:44.081 21903-21903/zhangphil.book D/输出: onNext:15
01-19 17:12:44.081 21903-21903/zhangphil.book D/输出: onNext:16
01-19 17:12:44.081 21903-21903/zhangphil.book D/输出: onNext:17
01-19 17:12:44.081 21903-21903/zhangphil.book D/输出: onNext:18
01-19 17:12:44.081 21903-21903/zhangphil.book D/输出: onNext:19
01-19 17:12:44.081 21903-21903/zhangphil.book D/输出: onNext:20
01-19 17:12:44.081 21903-21903/zhangphil.book D/输出: onNext:21
01-19 17:12:44.081 21903-21903/zhangphil.book D/输出: onNext:22
01-19 17:12:44.081 21903-21903/zhangphil.book D/输出: onNext:23
01-19 17:12:44.081 21903-21903/zhangphil.book D/输出: onNext:24
01-19 17:12:44.081 21903-21903/zhangphil.book D/输出: onNext:25
01-19 17:12:44.081 21903-21903/zhangphil.book D/输出: onNext:26
01-19 17:12:44.081 21903-21903/zhangphil.book D/输出: onNext:27
01-19 17:12:44.081 21903-21903/zhangphil.book D/输出: onNext:28
01-19 17:12:44.081 21903-21903/zhangphil.book D/输出: onNext:29
01-19 17:12:44.081 21903-21903/zhangphil.book D/输出: onNext:30
01-19 17:12:44.081 21903-21903/zhangphil.book D/输出: onNext:31
01-19 17:12:44.081 21903-21903/zhangphil.book D/输出: onNext:32
01-19 17:12:44.082 21903-21903/zhangphil.book D/输出: onNext:33
01-19 17:12:44.082 21903-21903/zhangphil.book D/输出: onNext:34
01-19 17:12:44.082 21903-21903/zhangphil.book D/输出: onNext:35
01-19 17:12:44.082 21903-21903/zhangphil.book D/输出: onNext:36
01-19 17:12:44.082 21903-21903/zhangphil.book D/输出: onNext:37
01-19 17:12:44.082 21903-21903/zhangphil.book D/输出: onNext:38
01-19 17:12:44.082 21903-21903/zhangphil.book D/输出: onNext:39
01-19 17:12:44.082 21903-21903/zhangphil.book D/输出: onNext:40
01-19 17:12:44.082 21903-21903/zhangphil.book D/输出: onNext:41
01-19 17:12:44.082 21903-21903/zhangphil.book D/输出: onNext:42
01-19 17:12:44.082 21903-21903/zhangphil.book D/输出: onNext:43
01-19 17:12:44.082 21903-21903/zhangphil.book D/输出: onNext:44
01-19 17:12:44.082 21903-21903/zhangphil.book D/输出: onNext:45
01-19 17:12:44.082 21903-21903/zhangphil.book D/输出: onNext:46
01-19 17:12:44.082 21903-21903/zhangphil.book D/输出: onNext:47
01-19 17:12:44.082 21903-21903/zhangphil.book D/输出: onNext:48
01-19 17:12:44.082 21903-21903/zhangphil.book D/输出: onNext:49
01-19 17:12:44.082 21903-21903/zhangphil.book D/输出: onNext:50
01-19 17:12:44.082 21903-21903/zhangphil.book D/输出: onNext:51
01-19 17:12:44.082 21903-21903/zhangphil.book D/输出: onNext:52
01-19 17:12:44.082 21903-21903/zhangphil.book D/输出: onNext:53
01-19 17:12:44.082 21903-21903/zhangphil.book D/输出: onNext:54
01-19 17:12:44.082 21903-21903/zhangphil.book D/输出: onNext:55
01-19 17:12:44.082 21903-21903/zhangphil.book D/输出: onNext:56
01-19 17:12:44.082 21903-21903/zhangphil.book D/输出: onNext:57
01-19 17:12:44.082 21903-21903/zhangphil.book D/输出: onNext:58
01-19 17:12:44.082 21903-21903/zhangphil.book D/输出: onNext:59
01-19 17:12:44.082 21903-21903/zhangphil.book D/输出: onNext:60
01-19 17:12:44.082 21903-21903/zhangphil.book D/输出: onNext:61
01-19 17:12:44.082 21903-21903/zhangphil.book D/输出: onNext:62
01-19 17:12:44.082 21903-21903/zhangphil.book D/输出: onNext:63
01-19 17:12:44.082 21903-21903/zhangphil.book D/输出: onNext:64
01-19 17:12:44.082 21903-21903/zhangphil.book D/输出: onNext:65
01-19 17:12:44.082 21903-21903/zhangphil.book D/输出: onNext:66
01-19 17:12:44.082 21903-21903/zhangphil.book D/输出: onNext:67
01-19 17:12:44.082 21903-21903/zhangphil.book D/输出: onNext:68
01-19 17:12:44.082 21903-21903/zhangphil.book D/输出: onNext:69
01-19 17:12:44.082 21903-21903/zhangphil.book D/输出: onNext:70
01-19 17:12:44.082 21903-21903/zhangphil.book D/输出: onNext:71
01-19 17:12:44.082 21903-21903/zhangphil.book D/输出: onNext:72
01-19 17:12:44.082 21903-21903/zhangphil.book D/输出: onNext:73
01-19 17:12:44.082 21903-21903/zhangphil.book D/输出: onNext:74
01-19 17:12:44.082 21903-21903/zhangphil.book D/输出: onNext:75
01-19 17:12:44.082 21903-21903/zhangphil.book D/输出: onNext:76
01-19 17:12:44.082 21903-21903/zhangphil.book D/输出: onNext:77
01-19 17:12:44.082 21903-21903/zhangphil.book D/输出: onNext:78
01-19 17:12:44.082 21903-21903/zhangphil.book D/输出: onNext:79
01-19 17:12:44.082 21903-21903/zhangphil.book D/输出: onNext:80
01-19 17:12:44.082 21903-21903/zhangphil.book D/输出: onNext:81
01-19 17:12:44.082 21903-21903/zhangphil.book D/输出: onNext:82
01-19 17:12:44.082 21903-21903/zhangphil.book D/输出: onNext:83
01-19 17:12:44.082 21903-21903/zhangphil.book D/输出: onNext:84
01-19 17:12:44.082 21903-21903/zhangphil.book D/输出: onNext:85
01-19 17:12:44.082 21903-21903/zhangphil.book D/输出: onNext:86
01-19 17:12:44.082 21903-21903/zhangphil.book D/输出: onNext:87
01-19 17:12:44.082 21903-21903/zhangphil.book D/输出: onNext:88
01-19 17:12:44.082 21903-21903/zhangphil.book D/输出: onNext:89
01-19 17:12:44.082 21903-21903/zhangphil.book D/输出: onNext:90
01-19 17:12:44.082 21903-21903/zhangphil.book D/输出: onNext:91
01-19 17:12:44.082 21903-21903/zhangphil.book D/输出: onNext:92
01-19 17:12:44.082 21903-21903/zhangphil.book D/输出: onNext:93
01-19 17:12:44.082 21903-21903/zhangphil.book D/输出: onNext:94
01-19 17:12:44.082 21903-21903/zhangphil.book D/输出: onNext:95
01-19 17:12:44.082 21903-21903/zhangphil.book D/输出: onNext:96
01-19 17:12:44.082 21903-21903/zhangphil.book D/输出: onNext:97
01-19 17:12:44.082 21903-21903/zhangphil.book D/输出: onNext:98
01-19 17:12:44.082 21903-21903/zhangphil.book D/输出: onNext:99
01-19 17:12:44.083 21903-21903/zhangphil.book D/输出: onComplete