Successfully reported this slideshow.
We use your LinkedIn profile and activity data to personalize ads and to show you more relevant ads. You can change your ad preferences anytime.

Meet RxJava, your new best friend!

4,215 views

Published on

The Reactive Extensions started out life for .NET but have now been ported to Java by Netflix! They were designed to easily and reliably model data in motion as streams that can be combined, filtered and queried. In this talk, Sam will introduce why Rx is a fantastic idea, how it can benefit not only your Android applications, but the way you program in general!

Published in: Technology
  • Be the first to comment

Meet RxJava, your new best friend!

  1. 1. Rxjava Your new best friend! @samhogy
  2. 2. String getName(); Iterable<String> getNames(); Synchronous blocking TIGHTLY-COUPLED
  3. 3. How can we model asynchronous data? Q:
  4. 4. Observable subscribe(Observer) Observer update() 1 * publisher Subscriber
  5. 5. RXJava ReactiveX / RxJava compile ‘io.reactivex:rxjava:1.0.0-rc.3’
  6. 6. Observable<T> subscribe(Observer<T>) Observer<T> onNext(T item) onCompleted() onError(Exception e) 1 *
  7. 7. Zero or more onNext Followed optionally by either onCompleted or onError
  8. 8. h e l l o ! X: 50 X: 55 X: 60 X: 65 y: 50 y: 60 y: 70 y: 75 X ALMOST ANYTHING CAN BE OBSERVABLE!
  9. 9. How can we model asynchronous data? Q: a: RxJava Observables
  10. 10. Observable<String> getNames(); aSynchronous Non-blocking LOOSELY-COUPLED
  11. 11. Observable.just(“hello”); String[] names = { “Alice”, “Bob”, “Sue” }; Observable.from(names); Observable.create<String>( -> { for (int i = 0; i < 50; i++) { } } ); observer observer.onNext(i); observer.onCompleted();
  12. 12. String[] names = { “Alice”, “Bob”, “Sue” }; Observable.from(names) .subscribe(new () { }); Action1<String> @Override public void call(String s) { System.out.println(“Ohai ” + s); } Listener (inner class)
  13. 13. String[] names = { “Alice”, “Bob”, “Sue” }; Observable.from(names) .subscribe( ); s -> System.out.println(“Ohai ” + s) Lambda expression
  14. 14. Observables are composable SEQUENCES CAN BE TRANSFORMED, FILTERED AND COMBINED
  15. 15. .buffer(200, Units.Milliseconds, 2) .. .map(x -> x.size()) 2 .filter(x -> x > 1) 2 . 1 .. .. 2 2 2 2 Mouse Clicks
  16. 16. Iterable vs Observable T next() throw Exception return onNext (T item) onError (Exception) onCompleted()
  17. 17. functional reactive programming Create observables Transform as necessary Subscribe to them do something when data arrives
  18. 18. Controlling Concurrency Scheduler computation() from(Executor e) immediate() io() newThread() test() trampoline() SUBSCRIBE ON & OBSERVE ON
  19. 19. RUNS ON A BACKGROUND THREAD String[] names = { “Alice”, “Bob”, “Sue” }; Observable.from(names) .subscribeOn( Schedulers.newThread() ) .observeOn( AndroidSchedulers.mainThread() ) .subscribe ( s -> System.out.println(s) ); RUNS ON UI THREAD
  20. 20. TestScheduler advanceTimeBy(long time, TimeUnit unit) advanceTimeTo(long time, TimeUnit unit) triggerActions() Test with virtual time
  21. 21. Rx-Android ReactiveX / RxAndroid compile ‘com.netflix.java:rxjava-android:0.10.1’
  22. 22. AndroidSchedulers static Scheduler mainThread() static Scheduler handlerThread(final Handler h)
  23. 23. bindActivity (Activity a, Observable<T> source) bindFragment (Object f, Observable<T> source) Observes on ui thread No notifications forwarded if scheduled to finish
  24. 24. Observable<T> clicks(T view, bool emitInitial) Observable<T> text(T input, bool emitInitial) TURN events from your views into observables
  25. 25. Can I use Java 8 on Android? Q: a: Retrolambda orfjackal / retrolambda
  26. 26. Where Next? • introToRx.com – Lee Campbell • Rx Java Wiki pages on Github • Learning RxJava for Android by example: github.com/Kaushikgopal/Android-Rx-Java

×