|
27 | 27 | import rx.Subscription; |
28 | 28 | import rx.functions.Action0; |
29 | 29 | import rx.functions.Action1; |
| 30 | +import rx.functions.Func1; |
30 | 31 | import rx.schedulers.Schedulers; |
31 | 32 | import rx.subscriptions.CompositeSubscription; |
32 | 33 | import timber.log.Timber; |
@@ -35,7 +36,7 @@ public class PollingFragment |
35 | 36 | extends BaseFragment { |
36 | 37 |
|
37 | 38 | public static final int INITIAL_DELAY = 0; |
38 | | - public static final int POLLING_INTERVAL = 1000; |
| 39 | + public static final int POLLING_INTERVAL = 500; |
39 | 40 | @Bind(R.id.list_threading_log) ListView _logsList; |
40 | 41 |
|
41 | 42 | private LogAdapter _adapter; |
@@ -72,28 +73,27 @@ public void onDestroy() { |
72 | 73 | @OnClick(R.id.btn_start_simple_polling) |
73 | 74 | public void onStartSimplePollingClicked() { |
74 | 75 | _setupLogger(); |
75 | | - _log(String.format("Simple String polling - %s", _counter)); |
76 | | - _subscriptions.add(Observable.create( |
77 | | - new Observable.OnSubscribe<String>() { |
78 | | - @Override |
79 | | - public void call(final Subscriber<? super String> subscriber) { |
80 | | - Subscription subscription = _worker |
81 | | - .schedulePeriodically(new Action0() { |
82 | | - @Override |
83 | | - public void call() { |
84 | | - subscriber.onNext(_doNetworkCallAndGetStringResult()); |
85 | | - } |
86 | | - }, INITIAL_DELAY, POLLING_INTERVAL, TimeUnit.MILLISECONDS); |
87 | | - subscriber.add(subscription); |
88 | | - } |
89 | | - }) |
90 | | - .take(10) |
91 | | - .subscribe(new Action1<String>() { |
92 | | - @Override |
93 | | - public void call(String s) { |
94 | | - _log(String.format("String polling - %s", s)); |
95 | | - } |
96 | | - }) |
| 76 | + |
| 77 | + _subscriptions.add(// |
| 78 | + Observable.interval(INITIAL_DELAY, POLLING_INTERVAL, TimeUnit.MILLISECONDS) |
| 79 | + .map(new Func1<Long, String>() { |
| 80 | + @Override |
| 81 | + public String call(Long heartBeat) { |
| 82 | + return _doNetworkCallAndGetStringResult(); |
| 83 | + } |
| 84 | + }).take(5) |
| 85 | + .doOnSubscribe(new Action0() { |
| 86 | + @Override |
| 87 | + public void call() { |
| 88 | + _log(String.format("Simple String polling - %s", _counter)); |
| 89 | + } |
| 90 | + }) |
| 91 | + .subscribe(new Action1<String>() { |
| 92 | + @Override |
| 93 | + public void call(String s) { |
| 94 | + _log(String.format("Start simple polling - %s", s)); |
| 95 | + } |
| 96 | + }) |
97 | 97 | ); |
98 | 98 | } |
99 | 99 |
|
|