Loading presentation...

Present Remotely

Send the link below via email or IM

Copy

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.

DeleteCancel

Make your likes visible on Facebook?

Connect your Facebook account to Prezi and let your likes appear on your timeline.
You can change this under Settings & Account at any time.

No, thanks

Be Reactive with Retrofit, Part II

No description
by

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
@colinmlee
Retrofit
A networking library created by Square to simplify interactions with any API server.
A simple client and Java interface for any networking API.
Retrofit
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 {
@GET("/users/{user}/repos")
void listRepos(@Path("user") String user,
Callback<List<Repo>> callback);
}
Observable Interface
public interface GitHubService {
@GET("/users/{user}/repos")
Observable<List<Repo>>
listRepos(@Path("user") String user);
}
Subscriptions
Subscribing to a Request
githubClient.listRepos("colintheshots")
.observeOn(AndroidSchedulers.mainThread())
.subscribe(new Action1<List<Gist>>() {
@Override
public void call(List<Gist> gistList) {
}
},new Action1<Throwable>() {
@Override
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.
.subscribeOn()
Determines the thread or scheduler used to perform initial background work.
Useful before switching back to the UI thread to perform view updates.
.observeOn()
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.
Deadlocks
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.
Backpressure
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
Composable
Network requests can be manipulated and chaned with extremely useful operators like retry and timeout.
Thread Control
Error Handling
Cancellation
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
http://goo.gl/vtpM6A
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.
http://goo.gl/4KDw2P
.zip() API Mashup
Follow the steps in the comments.

Let's take five minutes to play.
http://goo.gl/IOJkrm
Autocomplete with Delay
Follow the steps in the comments.

Let's take five minutes to play.
http://goo.gl/P2iqZI
Questions?
Full transcript