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

相关文章
相关标签/搜索