A practical guide to using RxJava on Android. Tips for improving your app architecture with reactive programming. What are the advantages and disadvantages of using RxJava over standard architecture? And how to connect with other popular Android libraries?
Presented at GDG DevFest The Netherlands 2016.
19. RxJava 1 vs. RxJava 2
• RxJava 2 in RC now
• limited support in libraries
• they will coexist for some time
• different group ids
• io.reactivex vs. io.reactivex.rxjava2
• different package names
• rx vs. io.reactivex
• https://github.com/ReactiveX/RxJava/wiki/What's-different-in-2.0
74. RxLifecycle
• auto unsubscribe based on Activity/Fragment
lifecycle
myObservable
.compose(
RxLifecycle.bindUntilEvent(
lifecycleObservable, ActivityEvent.DESTROY
)
)
.subscribe(…);
myObservable
.compose(RxLifecycle.bindActivity(lifecycleObs))
.subscribe(…);
75. RxLifecycle
• How to obtain ActivityEvent or
FragmentEvent?
A. rxlifecycle-components + subclass
RxActivity, RxFragment
B. Navi + rxlifecycle-navi
C. Write it yourself
76. RxLifecycle
public class MyActivity extends RxActivity {
@Override
public void onResume() {
super.onResume();
myObservable
.compose(bindToLifecycle())
.subscribe();
}
}
91. Retrofit
• sync or async API (Retrofit 2)
@GET("group/{id}/users")
Call<List<User>> groupList(@Path("id") int groupId);
@GET("group/{id}/users")
Observable<List<User>> groupList(@Path("id") int groupId);
• reactive API
92. Retrofit
• onNext() with Response, then onComplete()
• onError() in case of error
@GET("/data")
Observable<Response> getData(
@Body DataRequest dataRequest);