Upgrade to Pro — share decks privately, control downloads, hide ads and more …

RxJava での Observable のつくりかた

Pine Mizune
November 18, 2015

RxJava での Observable のつくりかた

社内 ReactiveX 勉強会での発表資料
ソースコード: https://github.com/pine613/rxnight

Pine Mizune

November 18, 2015
Tweet

More Decks by Pine Mizune

Other Decks in Programming

Transcript

  1. just (1/2) Observable .just("yano") .subscribe(new Action1<String>() { @Override public void

    call(String name) { // Hello yano System.out.println("Hello, " + name + "!"); } });
  2. just (2/2) Observable.create(new Observable.OnSubscribe<String>() { @Override public void call(final Subscriber<?

    super String> observer) { observer.onNext("yano"); observer.onCompleted(); } }).subscribe(new Action1<String>() { @Override public void call(String name) { System.out.println("Hello, " + name + "!"); // Hello yano } });
  3. empty Observable.<String>empty().doOnCompleted(new Action0() { @Override public void call() { System.out.println("Completed");

    } }).subscribe(new Action1<String>() { @Override public void call(String name) { // not performed ... System.out.println("Hello, " + name + "!"); } });
  4. range Observable .range(1, 10) .subscribe(new Action1<Integer>() { @Override public void

    call(Integer value) { System.out.println(value); // 1, 2, ..., 10 } });
  5. range Observable.range(1, 10).map(new Func1<Integer, Integer>() { @Override public Integer call(Integer

    value) { return value * 2; // 2, 4, 6, ... } }) .reduce(0, new Func2<Integer, Integer, Integer>() { // sum @Override public Integer call(Integer sum, Integer value) { return sum + value; } }).subscribe(new Action1<Integer>() { @Override public void call(Integer value) { System.out.println(value); // 110 } });
  6. repeat (1/2) Observable .just(1) .repeat(3) .subscribe(new Action1<Integer>() { @Override public

    void call(Integer value) { System.out.println(value); // 1, 1, 1 } });
  7. repeat (2/2) Observable .from(new Boolean[]{ true, false }) .repeat(3) .subscribe(new

    Action1<Boolean>() { @Override public void call(Boolean value) { System.out.println(value); // true, false, ... } });
  8. create (1/2) Observable<Integer> random = Observable.create( new Observable.OnSubscribe<Integer>() { @Override

    public void call(final Subscriber<? super Integer> observer) { Random random = new Random(); observer.onNext(random.nextInt(10)); // 0 ~ 9 observer.onCompleted(); } });
  9. create (2/2) Observable<Integer> random = Observable.create(new Observable.OnSubscribe<Integer>() { @Override public

    void call(final Subscriber<? super Integer> observer) { Random random = new Random(); observer.onNext(random.nextInt(10)); // 0 ~ 9 observer.onCompleted(); } }); random.repeat().take(10).subscribe(new Action1<Integer>() { @Override public void call(Integer value) { System.out.println(value); // 7, 0, 8, ... } });