Skip to content

Commit f3579c3

Browse files
author
Kaushik Gopal
committed
feat: add example for polling a server constantly
1 parent 66ceea9 commit f3579c3

4 files changed

Lines changed: 46 additions & 7 deletions

File tree

README.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,11 @@ Auto-updating views are a pretty cool thing. If you've dealt with Angular JS bef
6060

6161
While the example here is pretty rudimentary, the technique used to achieve the double binding using a `Publish Subject` is much more interesting.
6262

63+
### Polling with Schedulers
64+
65+
This is an example of polling using RxJava Schedulers. This is useful in cases, where you want to constantly poll a server and possibly get new data. The network call is "simulated" so it forces a delay before return a resultant string.
66+
67+
6368
## Work in Progress:
6469

6570
Examples that I would like to have here, but haven't found the time yet to flush out.

app/src/main/java/com/morihacky/android/rxjava/PollingFragment.java

Lines changed: 32 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -10,26 +10,33 @@
1010
import android.view.View;
1111
import android.view.ViewGroup;
1212
import android.widget.ArrayAdapter;
13-
import android.widget.Button;
1413
import android.widget.ListView;
1514
import butterknife.ButterKnife;
1615
import butterknife.InjectView;
1716
import butterknife.OnClick;
1817
import com.morihacky.android.rxjava.app.R;
1918
import java.util.ArrayList;
2019
import java.util.List;
20+
import java.util.concurrent.TimeUnit;
21+
import rx.Observable;
22+
import rx.Subscriber;
23+
import rx.functions.Action0;
24+
import rx.functions.Action1;
25+
import rx.schedulers.Schedulers;
2126
import rx.subscriptions.CompositeSubscription;
2227
import timber.log.Timber;
2328

2429
public class PollingFragment
2530
extends Fragment {
2631

32+
public static final int INITIAL_DELAY = 0;
33+
public static final int POLLING_INTERVAL = 1000;
2734
@InjectView(R.id.list_threading_log) ListView _logsList;
28-
@InjectView(R.id.btn_start_simple_polling) Button _btnSimplePolling;
2935

3036
private LogAdapter _adapter;
3137
private List<String> _logs;
3238
private CompositeSubscription _subscriptions;
39+
private int _counter = 0;
3340

3441
@Override
3542
public void onDestroy() {
@@ -40,6 +47,7 @@ public void onDestroy() {
4047
@Override
4148
public void onActivityCreated(@Nullable Bundle savedInstanceState) {
4249
super.onActivityCreated(savedInstanceState);
50+
_subscriptions = new CompositeSubscription();
4351
_setupLogger();
4452
}
4553

@@ -54,21 +62,39 @@ public View onCreateView(LayoutInflater inflater,
5462

5563
@OnClick(R.id.btn_start_simple_polling)
5664
public void onStartSimplePollingClicked() {
57-
65+
_subscriptions.add(Observable.create(new Observable.OnSubscribe<String>() {
66+
@Override
67+
public void call(final Subscriber<? super String> observer) {
68+
69+
Schedulers.newThread().createWorker() //
70+
.schedulePeriodically(new Action0() {
71+
@Override
72+
public void call() {
73+
observer.onNext(_doNetworkCallAndGetStringResult());
74+
}
75+
}, INITIAL_DELAY, POLLING_INTERVAL, TimeUnit.MILLISECONDS);
76+
}
77+
}).take(10).subscribe(new Action1<String>() {
78+
@Override
79+
public void call(String s) {
80+
_log(String.format("String polling - %s", s));
81+
}
82+
}));
5883
}
5984

60-
6185
// -----------------------------------------------------------------------------------
6286
// Method that help wiring up the example (irrelevant to RxJava)
6387

64-
private void _doSomeLongOperation_thatBlocksCurrentThread() {
65-
_log("performing long operation");
88+
private String _doNetworkCallAndGetStringResult() {
6689

6790
try {
6891
Thread.sleep(3000);
6992
} catch (InterruptedException e) {
7093
Timber.d("Operation was interrupted");
7194
}
95+
_counter++;
96+
97+
return String.valueOf(_counter);
7298
}
7399

74100
private void _log(String logMsg) {

app/src/main/res/layout/fragment_polling.xml

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,14 @@
2525
android:layout_marginLeft="20dp"
2626
android:textSize="16sp"
2727
android:text="Start simple polling"/>
28+
29+
<Button
30+
android:id="@+id/btn_start_increasingly_delayed_polling"
31+
android:layout_height="wrap_content"
32+
android:layout_width="wrap_content"
33+
android:layout_marginLeft="20dp"
34+
android:textSize="16sp"
35+
android:text="Start increasingly delayed polling"/>
2836
</LinearLayout>
2937

3038
<ListView

app/src/main/res/values/strings.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111
<string name="msg_demo_timeout">This is a demo of terminating events, that take too long to process. Events in this demo should timeout in 3 seconds. Button 1 is an event that takes 2s to process, and Button 2 is a n event that takes 5s to process. Hit Button 2, and see in the logs that it\'s been cancelled, while this is not the case for Button 1.</string>
1212
<string name="msg_demo_retrofit">Retrofit from Square is a super easy networking helper library. It works really well with RxJava and these are examples taken from Jake Wharton\'s talk at Netflix (see README). Really the only interesting bits here are in the code and logs.</string>
1313
<string name="msg_demo_doublebinding">Watch how the result gloriously auto-updates based on your changing inputs. Using a technique like this, you could achieve the two-way binding in Angular Js, or more efficiently use a pattern like the Presentation View Model.</string>
14-
<string name="msg_demo_polling">This is demo of polling or making a call repeatedly with RxJava. \n\nSimple polling: Notice in the logs how a network call (simulated) is repeatedly made in the background. \n\nExponential backoff polling: We start polling but in increasing time intervals depending on a provided condition.</string>
14+
<string name="msg_demo_polling">This is demo of polling or making a call repeatedly with RxJava. \n\nSimple polling: Notice in the logs how a network call (simulated) is repeatedly made in the background.</string>
1515

1616
<string name="btn_demo_schedulers">bg work (schedulers &amp; concurrency)</string>
1717
<string name="btn_demo_buffer">accumulate calls (buffer)</string>

0 commit comments

Comments
 (0)