2017-08-11 48 views
-3

如何取消訂閱RxJava2中的可觀察或訂閱?或者我真的需要嗎?如何取消訂閱RxJava2中的可觀察或訂閱?或者我真的需要嗎?

//Initialising Obersvable   
    Observable<SearchSuggestions> observable = NetworkContext.api.getSearch(""); 
     //subscribing obersvable on another thread and observing on main thread 

      observable.subscribeOn(Schedulers.io()) 
        .observeOn(AndroidSchedulers.mainThread()) 
        .subscribe(new Consumer<SearchSuggestions>() { 
         @Override 
         public void accept(SearchSuggestions searchSuggestions) throws Exception { 
    // here I receive the fetched results 

         } 
        }, new Consumer<Throwable>() { 
         @Override 
         public void accept(Throwable throwable) throws Exception { 
    //here the error 
         } 
        }); 
+0

請閱讀[this](https://github.com/ReactiveX/RxJava/wiki/What's-different-in-2.0#subscriber)。 – akarnokd

回答

0

要取消訂閱用戶,也許你應該申報

要取消訂閱只需撥打任何時間,但我會建議要取消訂閱的onDestroy(活動)或onDestroyView(片段)

if (subscription != null && subscription.isUnsubscribed()) { 
     subscription.unsubscribe(); 
} 
+0

它不在rxJava2中 –