RxJava/RxAndroid : buffer(int count)

RxJava缓冲操作
本文介绍了一个使用RxJava/RxAndroid实现的数据缓冲示例。通过buffer操作符将一系列整数按指定数量分组,并在主线程中观察这些分组的数据。示例展示了如何创建一个观察者并设置其回调方法以处理接收到的数据。

RxJava/RxAndroid : buffer(int count)

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;

import java.util.List;

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[]{0, 1, 2, 3, 4};

        Observable.fromArray(numbers)
                .buffer(2)
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribeWith(mObserver);
    }
}



output:

01-19 17:02:45.736 18276-18276/? D/输出: onNext
01-19 17:02:45.736 18276-18276/? D/输出: onNext:0
01-19 17:02:45.736 18276-18276/? D/输出: onNext:1
01-19 17:02:45.736 18276-18276/? D/输出: onNext
01-19 17:02:45.736 18276-18276/? D/输出: onNext:2
01-19 17:02:45.736 18276-18276/? D/输出: onNext:3
01-19 17:02:45.736 18276-18276/? D/输出: onNext
01-19 17:02:45.736 18276-18276/? D/输出: onNext:4
01-19 17:02:45.736 18276-18276/? D/输出: onComplete

// // 摘要: // Read bytes until an END indicator or termination character is reached. // // 返回结果: // An array of the bytes read. byte[] Read(); // // 摘要: // Read count bytes, or until an END indicator or termination character is reached, // whichever comes first. // // 参数: // count: // The maximum number of bytes to read. // // 返回结果: // An array of the bytes read. byte[] Read(long count); // // 摘要: // Read count bytes, or until an END indicator or termination character is reached, // whichever comes first. // // 参数: // count: // The maximum number of bytes to read. // // readStatus: // A status that indicates whether an END indicator or termination character terminated // the read operation. // // 返回结果: // An array of the bytes read. byte[] Read(long count, out ReadStatus readStatus); // // 摘要: // Read count bytes into the buffer buffer, starting at index, or until an END indicator // or termination character is reached, whichever comes first. // // 参数: // buffer: // An array of bytes allocated by the calling program. // // index: // The index in buffer to begin storing bytes // // count: // The maximum number of bytes to read. // // actualCount: // The actual count of bytes stored in buffer during the read operation. // // readStatus: // A status that indicates whether an END indicator or termination character terminated // the read operation. void Read(byte[] buffer, long index, long count, out long actualCount, out ReadStatus readStatus); // // 摘要: // Read count bytes into the buffer buffer, starting at index, or until an END indicator // or termination character is reached, whichever comes first. // // 参数: // buffer: // A reference to an array of bytes allocated by the calling program. // // index: // The index in buffer to begin storing bytes // // count: // The maximum number of bytes to read. // // actualCount: // The actual count of bytes stored in buffer during the read operation. // // readStatus: // A status that indicates whether an END indicator or termination character terminated // the read operation. unsafe void Read(byte* buffer, long index, long count, out long actualCount, out ReadStatus readStatus); // // 摘要: // Read bytes to buffer until an END indicator or termination character is reached. // // // 参数: // buffer: // A span of bytes backed by memory allocated by the calling program IVisaReadResult Read(Span<byte> buffer); 正确使用
09-02
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值