Log.v(“ThreeTips”, “#5”)
1- Retrofit: Dynamic Query Param
Let say you have a dynamic query structure for some retrofit services. Let say it is filter service and it filters questions by given query parameter. Query parameter can be “?exam_id=12” or “lecture_id=234”. You only know fired event which has given key and value and you want to call “/filter” service with that given event parameters.
You can’t do that:
@GET("filter")
Observable<QuestionWrapper> filter(@Query("exam_id") String examId);
Neither this:
@GET("filter")
Observable<QuestionWrapper> filter(@Query("lecture_id") String lectureId);
Because you don’t know what to put as query key. Retrofit has a way to do that. QueryMap. The solution is creating query map for this then give key and value to this map. Here is the result.
@GET("question")
Observable<QuestionWrapper> filter(@QueryMap Map<String, String> filterParam);
2- RxJava : Don’t repeat observeOn() and subscribeOn()
RxJava has Transformers. They are function that receives the current Observable and should return another Observable, possibly with given element type, in exchange that will be subscribed to by the downstream operators and subscribers.
So we all do that;
Observable.just("")
.subscribeOn(Schedulers.io())
.observeOn(Schedulers.computation())
.subscribe();
So Transformers come to survive. Here is a Utility class for Rx Transformation;
public class RxTransformer {
public static <T> Observable.Transformer<T, T> applyOnIO() {
return observable -> observable.subscribeOn(Schedulers.io())
.unsubscribeOn(Schedulers.io())
.observeOn(AndroidSchedulers.mainThread());
}
}
Now it is time to compose your observable;
apiSource.loadList().compose(RxTransformer.applyOnIO()).subscribe();
3- RxJava : Don’t forget to unsubscribe
We all doing some operation in our Presenters or ViewModels. Let’s talk about news feed. We make api call to get list of items from webservice then we are showing them on our Activity/Fragment. Meanwhile we have a cache mechanism which works on a background thread. While Cache mechanism doing its job, user try to load second page. Then suddenly user wants to go back or enter to detail page. What do you do. You should handle this situation. If you are using RxJava. It is so simple and elegant.
Create a CompositeSubscription in our constructor.
public class FeedViewModel {
CompositeSubscription subscriptions;
public RxTransformer() {
subscriptions = new CompositeSubscription();
}
}
Whenever we call an observable, add it to subscriptions.
subscriptions.add(accountManager.auth().subscribe());
subscriptions.add(feedUsecase.loadData().subscribe());
subscriptions.add(cacheUsecase.cache(data).subscribe());
Then you can unsubscribe from all when user (Call it onStop or onDestroy)
if(subscriptions != null && !subscriptions.isUnsubscribed())
subscriptions.unsubscribe();
Now all subscriptions are unsubscribed and will not complete its job when user exit.
You can think these like pseudocodes. Any can be syntax error on copy/paste.