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

Introduction to RxJava for Android Developers

Introduction to RxJava for Android Developers

This talk aims to introduce RxJava and the concepts of Reactive Programming to Android developers. We will focus on the Android world, its challenges and how to tackle them using “reactive” open source libraries like RxAndroid, RxLifecycle and RxBindings.

Marcelo Benites

October 12, 2016
Tweet

More Decks by Marcelo Benites

Other Decks in Programming

Transcript

  1. ReactiveX is a combination of the best ideas from the

    Observer pattern, the Iterator pattern, and functional programming
  2. Easily create event streams or data streams. CREATE Compose and

    transform streams with query-like operators. COMBINE
  3. Compose and transform streams with query-like operators. COMBINE Subscribe to

    any observable stream to perform side effects. LISTEN Easily create event streams or data streams. CREATE
  4. public interface Dog {
 
 public String getName();
 
 public

    boolean isTrained();
 } public interface DogHandler {
 
 Observable<Dog> trainDogs();
 }
  5. public class John implements DogHandler { 
 @Override public Observable<Dog>

    trainDogs() {
 return Observable.just(new DummyDog("Marley"));
 }
 }
  6. public class John implements DogHandler {
 
 @Override public Observable<Dog>

    trainDogs() {
 return Observable.from(
 Arrays.asList(new DummyDog("Marley"), 
 new DummyDog("Rocky"), 
 new DummyDog("Daisy")));
 }
 }
  7. public class John implements DogHandler {
 
 @Override public Observable<Dog>

    trainDogs() {
 return Observable.fromCallable(() -> { 
 return new DummyDog("Marley");
 });
 }
 }
  8. public class John implements DogHandler {
 
 @Override public Observable<Dog>

    trainDogs() {
 return Observable.defer(() -> {
 return Observable.just(new DummyDog("Marley"));
 });
 }
 }
  9. public class John implements DogHandler {
 
 @Override public Observable<Dog>

    trainDogs() {
 return Observable.create(new Observable.OnSubscribe<Dog>() {
 @Override public void call(Subscriber<? super Dog> subscriber) {
 subscriber.onNext(new DummyDog("Marley"));
 subscriber.onNext(new DummyDog("Rocky"));
 subscriber.onCompleted();
 }
 });
 }
 }
  10. public class John implements DogHandler {
 
 @Override public Observable<Dog>

    trainDogs() {
 return Observable.create(new Observable.OnSubscribe<Dog>() {
 @Override public void call(Subscriber<? super Dog> subscriber) {
 subscriber.onNext(new DummyDog("Marley"));
 subscriber.onNext(new DummyDog("Rocky"));
 subscriber.onCompleted();
 }
 });
 }
 }
  11. public class John implements DogHandler {
 
 @Override public Observable<Dog>

    trainDogs() {
 return Observable.from(
 Arrays.asList(new DummyDog("Marley"), 
 new DummyDog("Rocky"), 
 new DummyDog("Daisy")));
 }
 }
  12. public class John implements DogHandler {
 
 @Override public Observable<Dog>

    trainDogs() {
 return Observable.from( Arrays.asList(new DummyDog("Marley"), 
 new DummyDog("Rocky"), 
 new DummyDog("Daisy")))
 .map(dummyDog -> trainDog(dummyDog));
 }
 
 private Dog trainDog(Dog dog) {
 return new TrainedDog(dog.getName());
 }
 }
  13. public class John implements DogHandler {
 
 @Override public Observable<Dog>

    trainDogs() {
 return Observable.from( Arrays.asList(new DummyDog("Marley"), 
 new DummyDog("Rocky"), 
 new DummyDog("Daisy")))
 .map(dummyDog -> trainDog(dummyDog));
 }
 
 private Dog trainDog(Dog dog) {
 return new TrainedDog(dog.getName());
 }
 }
  14. public class John implements DogHandler {
 
 @Override public Observable<Dog>

    trainDogs() {
 return Observable.from( Arrays.asList(new DummyDog("Marley"), 
 new DummyDog("Rocky"), 
 new DummyDog(“Daisy"))) .filter(dummyDog -> !dummyDog.getName().equals(“Rocky"))
 .map(dummyDog -> trainDog(dummyDog));
 }
 
 private Dog trainDog(Dog dog) {
 return new TrainedDog(dog.getName());
 }
 }
  15. public class Mary implements DogHandler {
 
 @Override public Observable<Dog>

    trainDogs() {
 return Observable.just(new DummyDog("Rocky"))
 .map(dummyDog -> trainDog(dummyDog));
 }
 
 private Dog trainDog(Dog dog) {
 return new TrainedDog(dog.getName());
 }
 }
  16. public class John implements DogHandler {
 
 private final DogRepository

    dogRepository;
 
 public John(DogRepository dogRepository) {
 this.dogRepository = dogRepository;
 }
 
 @Override public Observable<Dog> trainDogs() {
 return dogRepository.storedDogs()
 .filter(storedDog -> (!storedDog.isTrained() 
 && !storedDog.getName().equals(“Rocky")))
 .map(dummyDog -> trainDog(dummyDog));
 }
 
 private Dog trainDog(Dog dummyDog) {
 return new TrainedDog(dummyDog.getName());
 }
 }
  17. Button button = (Button) findViewById(R.id.button);
 DogHandler john = DogHandlerFactory.get(DogHandlerFactory.JOHN);
 


    Observable<Dog> trainedDogs = RxView.clicks(button).flatMap(click -> john.trainDogs());
  18. Button button = (Button) findViewById(R.id.button);
 DogHandler john = DogHandlerFactory.get(DogHandlerFactory.JOHN);
 


    Observable<Dog> trainedDogs = RxView.clicks(button).concatMap(click -> john.trainDogs());
  19. trainedDogs.subscribe(new Observer<Dog>() {
 @Override public void onCompleted() {
 
 }


    
 @Override public void onError(Throwable e) {
 
 }
 
 @Override public void onNext(Dog dog) {
 
 }
 });
  20. Subscription subscription = trainedDogs
 .subscribe(new Observer<Dog>() {
 @Override public void

    onCompleted() {
 
 }
 
 @Override public void onError(Throwable e) {
 
 }
 
 @Override public void onNext(Dog dog) {
 
 }
 });
  21. @Override protected void onResume() {
 super.onResume();
 trainedDogs
 .compose(bindToLifecycle())
 .subscribe(new Observer<Dog>()

    {
 
 @Override public void onCompleted() {
 
 }
 
 @Override public void onError(Throwable e) {
 
 }
 
 @Override public void onNext(Dog dog) {
 
 }
 });
 }
  22. @Override protected void onResume() {
 super.onResume();
 trainedDogs
 .compose(bindUntilEvent(ActivityEvent.DESTROY))
 .subscribe(new Observer<Dog>()

    {
 
 @Override public void onCompleted() {
 
 }
 
 @Override public void onError(Throwable e) {
 
 }
 
 @Override public void onNext(Dog dog) {
 
 }
 });
 }
  23. RxAppCompatActivity view = MainActivity.this;
 view.lifecycle()
 .filter(activityEvent -> activityEvent.equals(ActivityEvent.RESUME))
 .flatMap(resume ->

    john.trainDogs())
 .compose(view.bindUntilEvent(ActivityEvent.DESTROY))
 .subscribe(new Observer<Dog>() {
 @Override public void onCompleted() {
 
 }
 
 @Override public void onError(Throwable e) {
 
 }
 
 @Override public void onNext(Dog dog) {
 
 }
 });
  24. RxAppCompatActivity view = MainActivity.this;
 view.lifecycle()
 .filter(activityEvent -> activityEvent.equals(ActivityEvent.RESUME))
 .flatMap(resume ->

    john.trainDogs())
 .compose(view.bindUntilEvent(ActivityEvent.DESTROY))
 .subscribe(new Observer<Dog>() {
 @Override public void onCompleted() {
 
 }
 
 @Override public void onError(Throwable e) {
 
 }
 
 @Override public void onNext(Dog dog) {
 
 }
 });
  25. RxAppCompatActivity view = MainActivity.this;
 view.lifecycle()
 .filter(activityEvent -> activityEvent.equals(ActivityEvent.RESUME))
 .flatMap(resume ->

    john.trainDogs())
 .compose(view.bindUntilEvent(ActivityEvent.DESTROY))
 .subscribe(new Observer<Dog>() {
 @Override public void onCompleted() {
 
 }
 
 @Override public void onError(Throwable e) {
 
 }
 
 @Override public void onNext(Dog dog) {
 
 }
 });
  26. RxAppCompatActivity view = MainActivity.this;
 view.lifecycle()
 .filter(activityEvent -> activityEvent.equals(ActivityEvent.RESUME))
 .flatMap(resume ->

    john.trainDogs())
 .compose(view.bindUntilEvent(ActivityEvent.DESTROY))
 .subscribe(new Observer<Dog>() {
 @Override public void onCompleted() {
 
 }
 
 @Override public void onError(Throwable e) {
 
 }
 
 @Override public void onNext(Dog dog) {
 
 }
 });
  27. RxAppCompatActivity view = MainActivity.this;
 view.lifecycle()
 .filter(activityEvent -> activityEvent.equals(ActivityEvent.RESUME))
 .flatMap(resume ->

    john.trainDogs())
 .compose(view.bindUntilEvent(ActivityEvent.DESTROY))
 .subscribe(new Observer<Dog>() {
 @Override public void onCompleted() {
 
 }
 
 @Override public void onError(Throwable e) {
 
 }
 
 @Override public void onNext(Dog dog) {
 
 }
 });
  28. RxAppCompatActivity view = MainActivity.this;
 view.lifecycle()
 .filter(activityEvent -> activityEvent.equals(ActivityEvent.RESUME))
 .flatMap(resume ->

    john.trainDogs())
 .compose(view.bindUntilEvent(ActivityEvent.DESTROY))
 .subscribe(new Observer<Dog>() {
 @Override public void onCompleted() {
 
 }
 
 @Override public void onError(Throwable e) {
 
 }
 
 @Override public void onNext(Dog dog) {
 
 }
 });
  29. RxAppCompatActivity view = MainActivity.this;
 view.lifecycle()
 .filter(activityEvent -> activityEvent.equals(ActivityEvent.RESUME))
 .compose(view.bindUntilEvent(ActivityEvent.DESTROY)) .flatMap(resume

    -> john.trainDogs())
 .subscribe(new Observer<Dog>() {
 @Override public void onCompleted() {
 
 }
 
 @Override public void onError(Throwable e) {
 
 }
 
 @Override public void onNext(Dog dog) {
 
 }
 });
  30. RxAppCompatActivity view = MainActivity.this;
 view.lifecycle()
 .filter(activityEvent -> activityEvent.equals(ActivityEvent.RESUME))
 .compose(view.bindUntilEvent(ActivityEvent.DESTROY)) .flatMap(resume

    -> john.trainDogs())
 .subscribe(new Observer<Dog>() {
 @Override public void onCompleted() {
 
 }
 
 @Override public void onError(Throwable e) {
 
 }
 
 @Override public void onNext(Dog dog) {
 
 }
 });
  31. public class John implements DogHandler {
 
 private final DogWebService

    dogWebService;
 
 public John(DogWebService dogWebService) {
 this.dogWebService = dogWebService;
 }
 
 @Override public Observable<Dog> trainDogs() {
 return Observable.fromCallable(() -> {
 return dogWebService.getDog();
 })
 .map(dummyDog -> trainDog(dummyDog));
 }
 
 private Dog trainDog(Dog dummyDog) {
 return new TrainedDog(dummyDog.getName());
 }
 }
  32. @Override public Observable<Dog> trainDogs() {
 return Observable.fromCallable(() -> {
 return

    dogWebService.getDog();
 })
 .map(dummyDog -> trainDog(dummyDog)) .subscribeOn(Schedulers.io());
 }
  33. @Override public Observable<Dog> trainDogs() {
 return Observable.fromCallable(() -> {
 return

    dogWebService.getDog();
 })
 .map(dummyDog -> trainDog(dummyDog)) .subscribeOn(Schedulers.io());
 }
  34. @Override public Observable<Dog> trainDogs() {
 return Observable.fromCallable(() -> {
 return

    dogWebService.getDog();
 })
 .map(dummyDog -> trainDog(dummyDog)) .subscribeOn(Schedulers.io()) .subscribeOn(Schedulers.computation());
 }
  35. @Override public Observable<Dog> trainDogs() {
 return Observable.fromCallable(() -> {
 return

    dogWebService.getDog();
 })
 .map(dummyDog -> trainDog(dummyDog)) .subscribeOn(Schedulers.io()) .subscribeOn(Schedulers.computation());
 }
  36. final TextView text = (TextView) findViewById(R.id.text);
 
 john.trainDogs()
 .subscribe(new Observer<Dog>()

    {
 @Override public void onCompleted() {
 
 }
 
 @Override public void onError(Throwable e) {
 
 }
 
 @Override public void onNext(Dog dog) {
 text.setText(dog.getName());
 }
 });
  37. final TextView text = (TextView) findViewById(R.id.text);
 
 john.trainDogs() .observeOn(AndroidSchedulers.mainThread())
 .subscribe(new

    Observer<Dog>() {
 @Override public void onCompleted() {
 
 }
 
 @Override public void onError(Throwable e) {
 
 }
 
 @Override public void onNext(Dog dog) {
 text.setText(dog.getName());
 }
 });
  38. final TextView text = (TextView) findViewById(R.id.text);
 
 john.trainDogs() .observeOn(AndroidSchedulers.mainThread())
 .subscribe(new

    Observer<Dog>() {
 @Override public void onCompleted() {
 
 }
 
 @Override public void onError(Throwable e) {
 
 }
 
 @Override public void onNext(Dog dog) {
 text.setText(dog.getName());
 }
 });
  39. final TextView text = (TextView) findViewById(R.id.text);
 
 john.trainDogs() .observeOn(AndroidSchedulers.mainThread()) .observeOn(Schedulers.io())


    .subscribe(new Observer<Dog>() {
 @Override public void onCompleted() {
 
 }
 
 @Override public void onError(Throwable e) {
 
 }
 
 @Override public void onNext(Dog dog) {
 text.setText(dog.getName());
 }
 });
  40. final TextView text = (TextView) findViewById(R.id.text);
 
 john.trainDogs() .observeOn(AndroidSchedulers.mainThread()) .observeOn(Schedulers.io())


    .subscribe(new Observer<Dog>() {
 @Override public void onCompleted() {
 
 }
 
 @Override public void onError(Throwable e) {
 
 }
 
 @Override public void onNext(Dog dog) {
 text.setText(dog.getName());
 }
 });
  41. final TextView text = (TextView) findViewById(R.id.text);
 
 john.trainDogs() .observeOn(AndroidSchedulers.mainThread()) .doOnNext(trainedDog

    -> text.setText(trainedDog.getName())) .observeOn(Schedulers.io())
 .subscribe(new Observer<Dog>() {
 @Override public void onCompleted() {
 
 }
 
 @Override public void onError(Throwable e) {
 
 }
 
 @Override public void onNext(Dog dog) {
 text.setText(dog.getName());
 }
 });
  42. final TextView text = (TextView) findViewById(R.id.text);
 
 john.trainDogs() .observeOn(AndroidSchedulers.mainThread()) .doOnNext(trainedDog

    -> text.setText(trainedDog.getName())) .observeOn(Schedulers.io())
 .subscribe(new Observer<Dog>() {
 @Override public void onCompleted() {
 
 }
 
 @Override public void onError(Throwable e) {
 
 }
 
 @Override public void onNext(Dog dog) {
 text.setText(dog.getName());
 }
 });
  43. final TextView text = (TextView) findViewById(R.id.text);
 
 john.trainDogs() .observeOn(AndroidSchedulers.mainThread()) .doOnNext(trainedDog

    -> text.setText(trainedDog.getName())) .delay(5, TimeUnit.SECONDS)
 .subscribe(new Observer<Dog>() {
 @Override public void onCompleted() {
 
 }
 
 @Override public void onError(Throwable e) {
 
 }
 
 @Override public void onNext(Dog dog) {
 text.setText(dog.getName());
 }
 });
  44. final TextView text = (TextView) findViewById(R.id.text);
 
 john.trainDogs() .observeOn(AndroidSchedulers.mainThread()) .doOnNext(trainedDog

    -> text.setText(trainedDog.getName())) .delay(5, TimeUnit.SECONDS)
 .subscribe(new Observer<Dog>() {
 @Override public void onCompleted() {
 
 }
 
 @Override public void onError(Throwable e) {
 
 }
 
 @Override public void onNext(Dog dog) {
 text.setText(dog.getName());
 }
 });
  45. john.trainDogs()
 .subscribe(new Observer<Dog>() {
 @Override public void onCompleted() {
 


    }
 
 @Override public void onError(Throwable e) {
 
 }
 
 @Override public void onNext(Dog dog) {
 
 }
 });
  46. john.trainDogs()
 .subscribe(new Observer<Dog>() {
 @Override public void onCompleted() {
 


    }
 
 @Override public void onError(Throwable e) {
 
 }
 
 @Override public void onNext(Dog dog) {
 
 }
 });
  47. john.trainDogs()
 .subscribe(new Observer<Dog>() {
 @Override public void onCompleted() {
 


    }
 
 @Override public void onError(Throwable e) {
 
 }
 
 @Override public void onNext(Dog dog) {
 
 }
 });