Skip to content

Commit c2ce991

Browse files
author
Kaushik Gopal
committed
fix: block duplicate calls + back pressure protect
1 parent 658a08e commit c2ce991

File tree

1 file changed

+18
-12
lines changed

1 file changed

+18
-12
lines changed

app/src/main/java/com/morihacky/android/rxjava/pagination/PaginationAutoFragment.java

Lines changed: 18 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@ public class PaginationAutoFragment extends BaseFragment {
3535
private PaginationAdapter _adapter;
3636
private RxBus _bus;
3737
private PublishSubject<Integer> _paginator;
38+
private boolean _requestUnderWay = false;
3839

3940
@Override
4041
public void onActivityCreated(@Nullable Bundle savedInstanceState) {
@@ -58,31 +59,36 @@ public void onStart() {
5859
_subscriptions = new CompositeSubscription();
5960

6061
Subscription s2 =//
61-
_paginator//
62-
.concatMap(nextPage -> _itemsFromNetworkCall(nextPage + 1, 10))//
62+
_paginator
63+
.onBackpressureDrop()
64+
.doOnNext(i -> _requestUnderWay = true)
65+
.concatMap(nextPage -> _itemsFromNetworkCall(nextPage + 1, 10))
6366
.observeOn(AndroidSchedulers.mainThread())
6467
.map(items -> {
6568
int start = _adapter.getItemCount() - 1;
6669

6770
_adapter.addItems(items);
6871
_adapter.notifyItemRangeInserted(start, 10);
6972

73+
_requestUnderWay = false;
7074
_progressBar.setVisibility(View.INVISIBLE);
7175
return null;
72-
})//
76+
})
7377
.subscribe();
7478

7579
// I'm using an Rxbus purely to hear from a nested button click
7680
// we don't really need Rx for this part. it's just easy ¯\_(ツ)_/¯
77-
Subscription s1 = _bus.asObservable().subscribe(event -> {
78-
if (event instanceof PaginationAdapter.ItemBtnViewHolder.PageEvent) {
79-
80-
// trigger the paginator for the next event
81-
int nextPage = _adapter.getItemCount() - 1;
82-
_paginator.onNext(nextPage);
83-
84-
}
85-
});
81+
Subscription s1 =//
82+
_bus.asObservable()
83+
.filter(o -> !_requestUnderWay)
84+
.subscribe(event -> {
85+
if (event instanceof PaginationAdapter.ItemBtnViewHolder.PageEvent) {
86+
87+
// trigger the paginator for the next event
88+
int nextPage = _adapter.getItemCount() - 1;
89+
_paginator.onNext(nextPage);
90+
}
91+
});
8692

8793
_subscriptions.add(s1);
8894
_subscriptions.add(s2);

0 commit comments

Comments
 (0)