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

Tango with RxJava

Tango with RxJava

DroidUp Tbilisi and Devfest 2016 presentation talk about RxJava essentials

Merab Tato Kutalia

October 23, 2016
Tweet

More Decks by Merab Tato Kutalia

Other Decks in Programming

Transcript

  1. Problems in app • callback-hell • manage tons of async

    code • error handling • code maintenance
  2. Rx

  3. “RxJava is a library that allows us to represent any

    operation as an asynchronous data stream that can be created on any thread, declaratively composed, and consumed by multiple objects on any thread.”
  4. “RxJava is a library that allows us to represent any

    operation as an asynchronous data stream that can be created on any thread, declaratively composed, and consumed by multiple objects on any thread.” ?
  5. What is reactive • programming with asynchronous data streams. •

    A stream is a sequence of ongoing events ordered in time.
  6. Why RxJava • Abstraction of complexity of async data flows

    and events • Filtering, composition, transformation • Error handling • Light and high performance
  7. Observables • This is the data source, the object that

    will be emitting the sequences of data items that the entities who are interested in the data they provide, will be subscribing. • It implements all the operators mentioned before to be able to transform the data streams. • Observables can be divided in two types: “Hot” observables which might emit data as soon as they are created and “Cold” observables which wait until an Observer subscribes to it.
  8. Observers Observers are consumers of an Observable’s asynchronous data stream.

    Observers can react to the data emitted by the Observable in whatever way they want.
  9. Subscribers Subscriber is just an Observer that can, among other

    things, unsubscribe itself from the items emitted by an Observable.
  10. Schedulers • Schedulers determine the thread on which Observables emit

    their asynchronous data streams and the thread on which Observers consume those data streams. • RxJava allows us to create and consume asynchronous data streams on any thread. Observable.create(new Observable.OnSubscribe<Story>() { //...
 })
 .subscribeOn(Schedulers.io())
 .observeOn(AndroidSchedulers.mainThread());
  11. Operators Creating Transforming Filtering Combining Error Handling Conditional Mathematical full

    list at http://reactivex.io/documentation/operators.html https://github.com/ReactiveX/RxJava/wiki/Alphabetical-List-of-Observable-Operators
  12. Operators Operators allow us to declaratively compose Observables An operator

    is applied to a “source” Observable and it returns a new Observable as a result
  13. Operators Observable.create(new Observable.OnSubscribe<Story>() {
 //Emitting story objects...
 })
 .map(new Func1<Story,

    String>() {
 @Override
 public String call(Story story) {
 return story.getTitle();
 }
 });
  14. Avoiding Memory Leaks Any call to Observable.subscribe() returns a Subscription

    @Override
 public void onCreate() { //...
 mSubscription = storiesObservable.subscribe(new Observer() {
 @Override
 public void onNext(Story story) {
 Log.d(TAG, story);
 }
 });
 }
 
 @Override
 public void onDestroy() {
 mSubscription.unsubscribe();
 }
  15. More for Android RxLifecycle is a project helps Android developers

    use RxJava within Activity without causing leaks. developed by Trello. https://github.com/trello/RxLifecycle RxBinding by Jake Wharton. Binding API’s for UI widgets. https://github.com/JakeWharton/RxBinding
  16. UNLEASH THE BEAST searchViewTextObservable.filter(new Func1<String, Boolean>() { //1
 @Override
 public

    Boolean call(String s) {
 return s.length() > 2;
 }
 })
 .debounce(QUERY_UPDATE_DELAY_MILLIS, TimeUnit.MILLISECONDS) //2
 .flatMap(new Func1<String, Observable<List<Story>>>() { //3
 @Override
 public Observable<List<Story>> call(String s) {
 return mAdapter.getSearchStoriesObservable(s);
 }
 }
 )
 .subscribeOn(Schedulers.io())
 .observeOn(AndroidSchedulers.mainThread())
 .subscribe(new Observer<List<Story>>() {
 //...
 @Override
 public void onNext(List<Story> stories) {
 mStoriesRecyclerView.setAdapter(new StoryAdapter(stories));
 }
 });
  17. UNLEASH THE BEAST searchViewTextObservable.filter(new Func1<String, Boolean>() { //1
 @Override
 public

    Boolean call(String s) {
 return s.length() > 2;
 }
 })
 .debounce(QUERY_UPDATE_DELAY_MILLIS, TimeUnit.MILLISECONDS) //2 .mergeWith(historicalQueryTappedObservable)
 .flatMap(new Func1<String, Observable<List<Story>>>() { //3
 @Override
 public Observable<List<Story>> call(String s) {
 return mAdapter.getSearchStoriesObservable(s);
 }
 }
 )
 .subscribeOn(Schedulers.io())
 .observeOn(AndroidSchedulers.mainThread())
 .subscribe(new Observer<List<Story>>() {
 //...
 @Override
 public void onNext(List<Story> stories) {
 mStoriesRecyclerView.setAdapter(new StoryAdapter(stories));
 }
 });
  18. Convert Async to Rx Observable.create(new Observable.OnSubscribe<String>() {
 @Override
 public void

    call(Subscriber<? super String> subscriber) {
 // Synchronous code written in AsyncTask’s doInBackground()
 }
 })
 .subscribeOn(Schedulers.newThread())
 .observeOn(AndroidSchedulers.mainThread())
 .subscribe();
  19. See More https://github.com/tatocaster/TangoWithRxJava Repo contains a very dumb and simple

    demonstration of operators and subscriptions (CompositeSubscription) and sample Unit Test
  20. People you should follow • David Karnok (project lead of

    RxJava) • Ivan Morgilo (Author of RxJava essentials) • Jake Wharton (Square) • Dan Lew (Trello) • Artem Zinnatulin
  21. Observable.just(“Questions?”)subscribe(new Observer<Object>() { @Override
 public void onNext(Object o) {
 Log.d(TAG,

    "onNext() called with: o = [" + o + "]");
 }
 @Override
 public void onCompleted() {
 Log.d(TAG, "Thanks");
 }
 @Override
 public void onError(Throwable e) {
 // ignore this shit
 }
 
 }); );