kaushikgopal / RxJava-Android-Samples

Learning RxJava for Android by example
Apache License 2.0
7.54k stars 1.37k forks source link

fix: finished polling demo #42

Closed marukami closed 8 years ago

marukami commented 8 years ago

Let me know if you think it's OK.

kaushikgopal commented 8 years ago

@marukami : been a crazy 2 weeks, so slowly punching through your great work.

some thoughts:

  1. i was working on a similar polling problem and realized that the solutions suggested (and also my original solution) are incredibly more complicated than they need to be. Observable.interval provides the "polling" functionality almost for free.
  2. onStartIncreasinglyDelayedPolling is super cool (thanks for the suggestion) but already represented in this exponential backoff example?
  3. I've been trying to move the examples slowly away from Observable.create unless it is the absolute best solution.

I'm pulling your PR down and making some changes, which i think should be a simpler solution to the polling problem. Please let me know what you think

marukami commented 8 years ago

@kaushikgopal no worries, it's been a bit wild for me also.

  1. The main issue with Observable.interval i had was it does not work for the task to finish before scheduling it again. This is fine if intervals are not close together, but I need to aggressively poll in some situations.
  2. I think it's fine removing onStartIncreasinglyDelayedPolling.
  3. I think it might be possible to use Observable.interval and have it wait for each action to finish. Looking at SchedulerWorkerTest as an example, I think it should possible with a custom scheduler.
kaushikgopal commented 8 years ago
  1. I think it's fine removing onStartIncreasinglyDelayedPolling.

Actually i think it would be fun to reimplement it here with repeatWhen instead of retryWhen. I'll update the PR.

  1. The main issue with Observable.interval i had was it does not work for the task to finish before scheduling it again. This is fine if intervals are not close together, but I need to aggressively poll in some situations.

Dang. You're totally right. I think we could still use a repeatWhen to handle that. Let me give it a try

kaushikgopal commented 8 years ago

actually doesn't appear to be a problem; even if the task takes long it waits until next is executed (as per a quick implementation). merging the PR with some changes, let me know if you see any issues.

marukami commented 8 years ago

even if the task takes long it waits until next is executed

You're right. It looks like Observable.interval does wait for the task to finish. Internally it uses an OnSubscribeTimerPeriodically and that calls schedulePeriodically. I can't remember what I was doing that broke it in my project. Oh well, I'll just update the project and see if it breaks again.

The example looks way better now. Thanks for fixing it up.

marukami commented 8 years ago

I worked out why just using interval does not work. While Interval itself will wait for each tick. When you map an Observable over Interval it does not. The map will not wait for each task to finish. I've uploaded an example in another branch here

I'll work on fix today and upload something tonight.

Thread.sleep 02-26 06:26:18.585 22212-22938/com.morihacky.android.rxjava I/PollingFragment$$M: Task: 0, WaitTime: 8sec 02-26 06:26:26.589 22212-22938/com.morihacky.android.rxjava I/PollingFragment$$M: Task: 1, WaitTime: 8sec 02-26 06:26:34.589 22212-22938/com.morihacky.android.rxjava I/PollingFragment$$M: Task: 2, WaitTime: 8sec 02-26 06:26:42.589 22212-22938/com.morihacky.android.rxjava I/PollingFragment$$M: Task: 3, WaitTime: 8sec 02-26 06:26:46.593 22212-22938/com.morihacky.android.rxjava I/PollingFragment$$M: Task: 4, WaitTime: 4sec 02-26 06:26:54.593 22212-22938/com.morihacky.android.rxjava I/PollingFragment$$M: Task: 5, WaitTime: 8sec 02-26 06:27:02.593 22212-22938/com.morihacky.android.rxjava I/PollingFragment$$M: Task: 6, WaitTime: 8sec 02-26 06:27:10.593 22212-22938/com.morihacky.android.rxjava I/PollingFragment$$M: Task: 7, WaitTime: 8sec

When sleeping the sequence is preserved.

If you return an Observable from the map of interval you get this. 02-26 05:22:28.529 22212-22938/com.morihacky.android.rxjava I/PollingFragment$6$$M: Task: 0, WaitTime: 4sec 02-26 05:22:32.521 22212-22938/com.morihacky.android.rxjava I/PollingFragment$6$$M: Task: 4, WaitTime: 4sec 02-26 05:22:33.521 22212-22938/com.morihacky.android.rxjava I/PollingFragment$6$$M: Task: 1, WaitTime: 8sec 02-26 05:22:33.537 22212-22938/com.morihacky.android.rxjava I/PollingFragment$6$$M: Task: 5, WaitTime: 4sec 02-26 05:22:34.521 22212-22938/com.morihacky.android.rxjava I/PollingFragment$6$$M: Task: 2, WaitTime: 8sec 02-26 05:22:35.521 22212-22938/com.morihacky.android.rxjava I/PollingFragment$6$$M: Task: 3, WaitTime: 8sec 02-26 05:22:35.541 22212-22938/com.morihacky.android.rxjava I/PollingFragment$6$$M: Task: 7, WaitTime: 4sec