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

RxJava and Groovy

RxJava and Groovy

The New York Times Android team's emphasis on reactive programming using RxJava and Groovy allowed us to gain power and expressiveness with very few lines of code while still supporting forward compatibility with Java 8. Senior software architect Mohit Pandey explains.

(Presented at the New York Android Developers meetup.)

More Decks by The New York Times Developers

Other Decks in Programming

Transcript

  1. @NYTDevs | developers.nytimes.com Project Phoenix • Complete rewrite ◦ code

    ◦ design • Leverage open source technologies (Picasso, Dagger, Robolectric) • Bleeding edge (RxJava, Groovy)
  2. @NYTDevs | developers.nytimes.com Code Structure • Each reusable component is

    a separate repo. Gradle submodule an option too. • Enforces modularization and provides clarity of dependencies. • Tests ( unit/contract/security) run only for the module that has changed. • Reactive Glue ( more on this later)
  3. @NYTDevs | developers.nytimes.com RxJava • Async data streams • Create,

    chain and filter streams • Error propagation • Schedulers ( RxAndroid) • Testing structures provided ◦ ex: toBlocking
  4. @NYTDevs | developers.nytimes.com RxJava by example networkManager.getData(source) .flatMap({ new FeedParser().getMovies(it)

    }) .observeOn(AndroidSchedulers.mainThread()) .subscribe({ Movies m -> movies.clear(); movies.addAll(m.movies); notifyDataSetChanged(); }, { Throwable e -> Toast.makeText(context,"Download failed: " + e.getMessage(),Toast.LENGTH_SHORT) .show(); }, { Log.d(this.getClass().getName(), "complete happened"); });
  5. @NYTDevs | developers.nytimes.com Reactive Glue - RxJava • Modules talk

    in terms of Observables • Promotes Async contracts • Tricky to wrap your head around non- blocking/async programming ◦ easier for mobile due to inherent async behavior
  6. @NYTDevs | developers.nytimes.com Thundering herd • Subjects becomes gatekeeper •

    Return subject and emit values to all awaiting clients • Protect your server from self inflicted DDoS
  7. @NYTDevs | developers.nytimes.com Groovy Func0 func = new Func0<string>() {

    @Override public String call() { return "my content"; } }; Async.start(func); With closures it becomes: Async.start({ return “my content” })
  8. @NYTDevs | developers.nytimes.com Groovy Challenges • Method number ( Dex

    limit hit more easily) • Hard to debug • Reliance on reflection leads to potential slowness • Jack & Jill • Code obfuscation ( reflection complicates things)
  9. @NYTDevs | developers.nytimes.com Other Challenges • Rewrite happening while Android

    tooling and Groovy in beta. • Dagger 1.0 to 2.0 forced upgrade due to code obfuscation problem
  10. @NYTDevs | developers.nytimes.com Leveraging Server side- Samizdat • Scala -

    Finagle • Content transformations • Streamlined auth • API centralization