RxJava的簡介和簡單使用

RxJava簡介:java

http://gank.io/post/560e15be2dca930e00da1083#toc_1ide

實例:post

public class MainActivity extends Activity {
 
  private Subscription subscription;
 
  @Override
  protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
 
    subscription = Observable
        .fromCallable(new Callable<Object>() {
          @Override
          public Object call() throws Exception {
            return doSomeStuff();
          }
        })
        .subscribeOn(Schedulers.io())
        .observeOn(AndroidSchedulers.mainThread())
        .subscribe(new Action1<Object>() {
          @Override
          public void call(Object o) {
            // adapt contents
          }
        });
  }
 
  private Object doSomeStuff() {
    //do something to get result
    return new Object();
  }
 
  @Override
  protected void onDestroy() {
    subscription.unsubscribe();
    super.onDestroy();
  }
 
}

注意若是咱們沒有unsubscribe Subscription那麼仍然可能會出現內存泄漏。code

相關文章
相關標籤/搜索