1. 程式人生 > >Rxjava2定時器週期執行任務,避免記憶體洩漏

Rxjava2定時器週期執行任務,避免記憶體洩漏

 private CompositeDisposable mCompositeDisposable = new CompositeDisposable();
    
    private void test() {
        mCompositeDisposable.add(Observable.interval(0, 3000, TimeUnit.MILLISECONDS)
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribeWith(getObserver()));
    }
    
    private DisposableObserver getObserver() {
        DisposableObserver disposableObserver = new DisposableObserver<Object>() {
            @Override
            public void onNext(Object o) {
                http();
            }
    
            @Override
            public void onComplete() {
            }
    
            @Override
            public void onError(Throwable e) {
            }
        };
    
        return disposableObserver;
    }
    
    @Override
        public void onDestroy() {
            super.onDestroy();
     		//view=null;//view = inflater.inflate(R.layout.battery_status, container, false);
            mCompositeDisposable.clear();
        }