Log.v(“ThreeTips”, “#9”)

Mert SIMSEK
2 min readDec 22, 2016

--

1- RxJava : repeatWhen & takeUntil explained

In this post I already gave a snippet about repeatWhen and takeUntil. I used these operations for long polling data from server. Let’s go over it again.

api.loadHotels(searchRequest)
.repeatWhen(obs -> observable.delay(3, TimeUnit.SECONDS))
.takeUntil(searchResponse -> searchResponse.isCompleted)
.subscribe(listener::hotelListLoaded);

So, What is happening there?

1- LoadHotel service is called

2- Just after that, repeatWhen operation is called (observable.delay() is prepared but not subscribed yet. It waits for onCompleted())

3- observable.delay(3, TimeUnit.SECONDS) observable waits for loadHotels() observable‘s onCompleted().

4- loadHotel response is fetched and onNext is called.

5- listener::hotelListLoaded is called because of onNext.

6- takeUntil checks if resubscribe is needed.

7- If no, repeatWhen is not called, and subscription completes.

8-If yes, then loadHotel is called again with delay.

9-Goes to step 4.

I already mentioned same observable chain previous blogpost, I know. I just wanted to explain details and go one step ahead. Now What if we want to add retry count? I mean we want this observable chain and we also want to try 10 times at most (If it is still continuing resubscribing.)

api.loadHotels(searchRequest)
.repeatWhen(obs -> observable
.delay(3, TimeUnit.SECONDS)
.zipWith(Observable.range(1, 10), (o, count) -> count))
.takeUntil(searchResponse -> searchResponse.isCompleted)
.subscribe(listener::hotelListLoaded);

We zipped our delayed observable with Observable.range(). When it tries 10 times, it calls onComplete So our loadHotel observable also completes. You can check also this blogpost helps me a lot to grasp. And also check comments below that blogpost ;)

2- Android Support Library : RecyclerView prefetch

There is an important changes in Support Library 25.1.0 about Nested Recyclerviews(for example, vertical scrolling list of horizontal scrolling lists). If your inner, horizontal lists show a minimum of three and a half item views at a time, you can improve performance by callingLinearLayoutManager.setInitialPrefetchItemCount(4). Doing so allows RecyclerView to create all relevant views early, while the outer RecyclerView is scrolling, which significantly reduces the amount of stuttering during scrolls. Chet has already explained how that works well here.

3- Theme’s primary color programatically

Sometimes we have different themes in style.xml. And sometimes we need to get primary color of current theme. Here is my util method for that.

public static int getPrimaryColor(Context context) {
TypedValue typedValue = new TypedValue();
TypedArray a = context.obtainStyledAttributes(typedValue.data, new int[] {R.attr.colorPrimary });
int color = a.getColor(0, 0);
a.recycle();
return color;
}

--

--

Mert SIMSEK
Mert SIMSEK

Responses (1)