2727import rx .Subscription ;
2828import rx .functions .Action0 ;
2929import rx .functions .Action1 ;
30+ import rx .functions .Func1 ;
3031import rx .schedulers .Schedulers ;
3132import rx .subscriptions .CompositeSubscription ;
3233import timber .log .Timber ;
@@ -35,7 +36,7 @@ public class PollingFragment
3536 extends BaseFragment {
3637
3738 public static final int INITIAL_DELAY = 0 ;
38- public static final int POLLING_INTERVAL = 1000 ;
39+ public static final int POLLING_INTERVAL = 500 ;
3940 @ Bind (R .id .list_threading_log ) ListView _logsList ;
4041
4142 private LogAdapter _adapter ;
@@ -72,28 +73,27 @@ public void onDestroy() {
7273 @ OnClick (R .id .btn_start_simple_polling )
7374 public void onStartSimplePollingClicked () {
7475 _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+ })
9797 );
9898 }
9999
0 commit comments