RxJava/RxAndroid : takeLast
package zhangphil.book;
import android.os.SystemClock;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
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<Integer> mObserver = new Observer<Integer>() {
@Override
public void onSubscribe(Disposable d) {
}
@Override
public void onNext(Integer i) {
Log.d(TAG, "onNext:" + 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[]{0, 1, 2, 3, 4};
Observable.fromArray(numbers)
.takeLast(2)
.subscribeOn(Schedulers.io())
.observeOn(AndroidSchedulers.mainThread())
.subscribeWith(mObserver);
}
}
output:
01-19 14:43:10.401 6018-6018/zhangphil.book D/输出: onNext:3
01-19 14:43:10.402 6018-6018/zhangphil.book D/输出: onNext:4
01-19 14:43:10.402 6018-6018/zhangphil.book D/输出: onComplete