您好,登錄后才能下訂單哦!
這期內(nèi)容當(dāng)中小編將會(huì)給大家?guī)碛嘘P(guān)使用RxJava怎么取消訂閱,文章內(nèi)容豐富且以專業(yè)的角度為大家分析和敘述,閱讀完這篇文章希望大家可以有所收獲。
Consumer類型
Observable創(chuàng)建返回Disposable取消
public class SecondActivity extends AppCompatActivity { private static final String TAG = "SecondActivity"; private Disposable disposable; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_second); disposable = Observable.create(new ObservableOnSubscribe<String>() { @Override public void subscribe(ObservableEmitter<String> emitter) throws Exception { try { Thread.sleep(5000); } catch (InterruptedException e) { e.printStackTrace(); } } }).subscribeOn(Schedulers.io()) .observeOn(AndroidSchedulers.mainThread()) .subscribe(new Consumer<String>() { @Override public void accept(String s) throws Exception { Log.d(TAG, "accept: "+s); } }); } @Override protected void onDestroy() { super.onDestroy(); Log.d(TAG, "onDestroy: "); //取消訂閱 if(disposable != null && !disposable.isDisposed()){ disposable.dispose(); Log.d(TAG, "onDestroy: dispose"); } } }
普通類型Observer
在Observer中獲取Disposable然后取消
public class ThirdActivity extends AppCompatActivity { private static final String TAG = "ThirdActivity"; Disposable disposable; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_third); Observable.create(new ObservableOnSubscribe<String>() { @Override public void subscribe(ObservableEmitter<String> emitter) throws Exception { try { Thread.sleep(5000); emitter.onNext("testInfo"); } catch (InterruptedException e) { e.printStackTrace(); } } }).subscribeOn(Schedulers.io()) .observeOn(AndroidSchedulers.mainThread()) .subscribe(new Observer<String>() { @Override public void onSubscribe(Disposable d) { disposable = d; } @Override public void onNext(String s) { Log.d(TAG, "onNext: "+s); } @Override public void onError(Throwable e) { Log.d(TAG, "onError: "); } @Override public void onComplete() { Log.d(TAG, "onComplete: "); } }); } @Override protected void onDestroy() { super.onDestroy(); Log.d(TAG, "onDestroy: "); //然后在需要取消訂閱的地方調(diào)用即可 if (disposable != null && !disposable.isDisposed()) { Log.d(TAG, "dispose: "); disposable.dispose(); } } }
DisposableObserver類型
利用DisposableObserver和SubscribeWith直接返回Disposable,然后取消
public class FourthActivity extends AppCompatActivity { private static final String TAG = "FourthActivity"; private DisposableObserver<String> observer; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_fourth); observer = Observable.create(new ObservableOnSubscribe<String>() { @Override public void subscribe(ObservableEmitter<String> emitter) throws Exception { try { Thread.sleep(5000); emitter.onNext("testInfo"); } catch (InterruptedException e) { e.printStackTrace(); } } }).subscribeOn(Schedulers.io()) .observeOn(AndroidSchedulers.mainThread()) .subscribeWith(new DisposableObserver<String>() { @Override public void onNext(String o) { Log.d(TAG, "onNext: "+o); } @Override public void onError(Throwable e) { Log.d(TAG, "onError: "); } @Override public void onComplete() { Log.d(TAG, "onComplete: "); } }); } @Override protected void onDestroy() { super.onDestroy(); if (observer != null && !observer.isDisposed()) { Log.d(TAG, "dispose: "); observer.dispose(); } } }
取消多個(gè)Observer
把多個(gè)Observer添加CompositeDisposable,一次取消
public class ComDisposableActivity extends AppCompatActivity { private Disposable disposable1; private Disposable disposable2; private static final String TAG = "ComDisposableActivity"; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_com_disposable); Observable.create(new ObservableOnSubscribe<String>() { @Override public void subscribe(ObservableEmitter<String> emitter) throws Exception { try { Thread.sleep(5000); emitter.onNext("testInfo"); } catch (InterruptedException e) { e.printStackTrace(); } } }).subscribeOn(Schedulers.io()) .observeOn(AndroidSchedulers.mainThread()) .doOnDispose(new Action() { @Override public void run() throws Exception { Log.d(TAG, "run: Unsubscribing subscription from onCreate()"); } }) .subscribe(new Observer<String>() { @Override public void onSubscribe(Disposable d) { disposable1 = d; } @Override public void onNext(String s) { Log.d(TAG, "onNext: "+s); } @Override public void onError(Throwable e) { Log.d(TAG, "onError: "); } @Override public void onComplete() { Log.d(TAG, "onComplete: "); } }); Observable.create(new ObservableOnSubscribe<String>() { @Override public void subscribe(ObservableEmitter<String> emitter) throws Exception { try { Thread.sleep(5000); emitter.onNext("testInfo"); } catch (InterruptedException e) { e.printStackTrace(); } } }).subscribeOn(Schedulers.io()) .observeOn(AndroidSchedulers.mainThread()) .subscribe(new Observer<String>() { @Override public void onSubscribe(Disposable d) { disposable2 = d; } @Override public void onNext(String s) { Log.d(TAG, "onNext: "+s); } @Override public void onError(Throwable e) { Log.d(TAG, "onError: "); } @Override public void onComplete() { Log.d(TAG, "onComplete: "); } }); } @Override protected void onDestroy() { super.onDestroy(); CompositeDisposable compositeDisposable = new CompositeDisposable(); //批量添加 compositeDisposable.add(disposable1); compositeDisposable.add(disposable2); //最后一次性全部取消訂閱 compositeDisposable.dispose(); } }
RxLifecyle取消
OnDestory取消
Observable.interval(1, TimeUnit.SECONDS) .doOnDispose(new Action() { @Override public void run() throws Exception { Log.d(TAG, "Unsubscribing bindToLifecycle from onDestroy()"); } }) .compose(this.<Long>bindToLifecycle()) .subscribe(new Consumer<Long>() { @Override public void accept(Long num) throws Exception { Log.d(TAG, "accept: " + num); } });
指定生命周期取消
Observable.interval(1,TimeUnit.SECONDS) .doOnDispose(new Action() { @Override public void run() throws Exception { Log.d(TAG, "Unsubscribing UbindUntilEvent from onPause()"); } }).compose(this.<Long>bindUntilEvent(ActivityEvent.PAUSE)) .subscribe(new Consumer<Long>() { @Override public void accept(Long aLong) throws Exception { Log.d(TAG, "bindUntilEvent accept: " + aLong); } });
上述就是小編為大家分享的使用RxJava怎么取消訂閱了,如果剛好有類似的疑惑,不妨參照上述分析進(jìn)行理解。如果想知道更多相關(guān)知識(shí),歡迎關(guān)注億速云行業(yè)資訊頻道。
免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。