truetime-android
truetime-android copied to clipboard
provide an example of a background service which repeatedly calls TrueTime.init in the background
A point that was raised in both https://github.com/instacart/truetime-android/issues/90 and https://github.com/instacart/truetime-android/issues/84.
It is necessary to repeatedly call the init api after regular intervals. This needs to be done because of the natural drift that comes up with clocks (google oscillator crystals and clock drift for more context).
- [x] make this more explicit in the README
- [ ] provide example code or better apis for doing this
ok added a wiki and this section in particular that should help explain some of this -> https://github.com/instacart/truetime-android/wiki/Caveats---Gotchas#need-for-re-syncing
Thinking aloud here:
Currently we have a Single
variant for TrueTimeRx.init
. My current thought process is that, that should remain but we also have an Observable
api exposed which does exactly what @scottybe expects it to do as mentioned in this issue.
Letting this thought marinate for a while before making changes.
I think repeatWhen { Observable.interval(duration, timeUnit) }
might work (haven't actually verified) for the variants other than initializeRx
because that method checks isInitialized
while the other two initialization methods will always re-run.
Maybe we could add some new methods; TruetimeRx.initializeXwithRefreshInterval(long duration, timeUnit TimeUnit, scheduler Scheduler)
? Kind of torn on that last param since it seems like just defaulting to the computation scheduler would handle 99% of use cases.
Not having a lot of experience in RXJava2, I came up with this Flowable which can be used in place of the TrueTimeRx Flowable directly. It reinitializes TrueTimeRx every 12 minutes and emits a new Date. Is this the right way to do it?
final TrueTimeRx trueTimeBuild = TrueTimeRx.build();
Flowable<Date> trueTimeRxFlowable = Flowable.create(emitter -> {
try {
Flowable.interval(0L, 12, TimeUnit.MINUTES).subscribe(tick -> {
trueTimeBuild.initializeRx("time.google.com")
.subscribeOn(Schedulers.io())
.subscribe(date -> {
emitter.onNext(date);
}, throwable -> {
Log.e(TAG, "+++ Could NOT initialize truetime!!!");
});
});
} catch (Exception e) {
emitter.onError(e);
}
}, BackpressureStrategy.BUFFER);
trueTimeRxFlowable.subscribe();