Loading presentation...

Present Remotely

Send the link below via email or IM


Present to your audience

Start remote presentation

  • Invited audience members will follow you as you navigate and present
  • People invited to a presentation do not need a Prezi account
  • This link expires 10 minutes after you close the presentation
  • A maximum of 30 users can follow your presentation
  • Learn more about this feature in our knowledge base article

Do you really want to delete this prezi?

Neither you, nor the coeditors you shared it with will be able to recover it again.


Be Reactive with Retrofit, Part II

No description

Colin Lee

on 10 November 2014

Comments (0)

Please log in to add your comment.

Report abuse

Transcript of Be Reactive with Retrofit, Part II

Be Reactive with Retrofit, Part II
A networking library created by Square to simplify interactions with any API server.
A simple client and Java interface for any networking API.
Retrofit + Reactive
Late last year, Jake Wharton introduced support for RxJava Observables in addition to the two existing options of synchronous or callback-based functions.
Callback Interface
public interface GitHubService {
void listRepos(@Path("user") String user,
Callback<List<Repo>> callback);
Observable Interface
public interface GitHubService {
listRepos(@Path("user") String user);
Subscribing to a Request
.subscribe(new Action1<List<Gist>>() {
public void call(List<Gist> gistList) {
},new Action1<Throwable>() {
public void call(Throwable throwable) {
Composite Subscriptions
A subscription object that represents a group of subscriptions to be unsubscribed together.
Useful for preventing memory leaks and stopping obsolete requests.
Determines the thread or scheduler used to perform initial background work.
Useful before switching back to the UI thread to perform view updates.
Determines the thread Retrofit will use to perform view updates.
Typically, AndroidSchedulers.mainThread() is a good choice.
Rx Pitfalls
Memory Leaks
Maintain composite subscriptions and unsubscribe when not using the subscriptions.
Never ignore the return value of .subscribe()!
Add it to your composite subscription.
Know which Rx operators block.
Do not alter a Subject from its observers.
Error Handling
Always set an onError Action function.
OnErrorResumeNext() allows you to set values to emit when an error occurs. This is useful for setting UI.
Android Lifecycle
Continuing a Subscription during a configuration change is possible using .cache() or .replay().
Set timeouts.
What is Backpressure?
When one source observable emits items at a faster pace than another, this can create backpressure.
Backpressure occurs often in networking code because one request may reliably be faster than another.
Handling Backpressure
Operators change the rate at which an Observable emits items: sample(), throttleLast(), throttleFirst(), throttleWithTimeout(), debounce(), buffer(), and window().
Other operators like zip() provide a default backpressure behavior.
Reactive Pull
The Subscriber.request() method allows one to switch from the default reactive push behavior to "reactive pull."
Call the request() method in onNext to pull additional items.
Adding Backpressure Handling
onBackpressureBuffer() adds reactive pull behavior to an existing Observable, buffering extra items.
onBackpressureDrop() does the same, except dropping extra items.
RxJava Retrofit Examples
Rx Benefits
Network requests can be manipulated and chaned with extremely useful operators like retry and timeout.
Thread Control
Error Handling
Canceling requests is trivial.
Nothing handles the results of multiple threads and thread pools more cleanly than RxJava.
Observables can be forced to be well-behaved in ways that callbacks cannot.
Error handling can be deferred to the onError function of a entire chain's subscription instead of having repetitive code.
CompositeSubscription and onComplete helps keep your program in a sane state.
One may chain and combine the results of multiple requests asynchronously.
GitHub Example
Follow the steps in the comments.

Let's take five minutes to play.
Watch Face
Follow the steps in the comments.

Let's take five minutes to play.
.zip() API Mashup
Follow the steps in the comments.

Let's take five minutes to play.
Autocomplete with Delay
Follow the steps in the comments.

Let's take five minutes to play.
Full transcript