Comments (10)
Found an dirty fix, by calling TrueTimeRx.build().withSharedPreferences(getApplicationContext()).isInitialized()
The issue was that there was no _sharedPreferences
in the DiskCacheClient
class.
from truetime-android.
@EdwardvanRaak
Actually no. This is the method I'm using in my Application class:
private void initTime() {
//noinspection AccessStaticViaInstance
if (TrueTimeRx.build().withSharedPreferences(getApplicationContext()).isInitialized()) {
Timber.d("initTime(): TrueTime is already initialized per this boot - using cache!");
return;
}
// This will make network calls to get the real time
TrueTimeRx.build()
.withLoggingEnabled(true)
.withSharedPreferences(getApplicationContext())
.initializeRx("time.google.com")
.subscribeOn(Schedulers.io())
.subscribe(date -> Timber.d("initTime(): TrueTime was initialized and we have a time: " + date),
throwable -> Timber.e(throwable,
"initTime(): Something went wrong when trying to initializeRx TrueTime"),
() -> Timber.d("initTime(): TrueTime finished!"));
}
If I use TrueTimeRx.isInitialized()
directly, it will return false due to the fact that it cannot access SharedPreferences
without an Context
.
Also, If I initialize the library without this check it will trigger the entire initialization process without checking if it is already available.
from truetime-android.
@IonutNegru87 I believe you meant TrueTimeRx.build().withSharedPreferences(context).initialize()
right? I had the same problem.
from truetime-android.
couple of things:
- @IonutNegru87 : we don't need the context really, we just need the shared preferences object to exist (i.e. be not null). when you build truetime this is stored as a reference.
This is where the logic is handled:
i would confirm the output there.
- i would think, this is all that you would need to do
private TrueTimeRx trueTime = TrueTimeRx.build()
.withSharedPreferences(getApplicationContext())
.withServerResponseDelayMax(250)
.withLoggingEnabled(true);
private void initTime() {
if (trueTime.isInitialized()) {
Timber.d("initTime(): TrueTime is already initialized per this boot - using cache!");
return;
}
// This will make network calls to get the real time
trueTime
.initializeRx("time.google.com")
.subscribeOn(Schedulers.io())
.subscribe(date -> Timber.d("initTime(): TrueTime was initialized and we have a time: " + date),
throwable -> Timber.e(throwable,
"initTime(): Something went wrong when trying to initializeRx TrueTime"),
() -> Timber.d("initTime(): TrueTime finished!"));
}
another thing you could do to debug, is actually check if the shared preferences holds the cached sntp time, after TT is being initialized.
from truetime-android.
Thanks for the thoughtful library!
I am also facing the problem of Time not surviving across app restarts. It always needs internet and disk cache doesn't seem to work across app restarts. I checked the shared preferences file. It had no entries.
I am using 2.2 version of truetime rx. Following is my init code for your reference.
Any help is much appreciated!
public static void initialize(Context context) {
if (TrueTime.build().withSharedPreferences(context).withLoggingEnabled(BuildConfig.DEBUG).isInitialized()) {
Log.d("truetime", "initTime(): TrueTime is already initialized per this boot - using cache!");
return;
}
if (!AppUtils.isInternetAvailable(context)) {
return;
}
// TrueTimeRx.clearCachedInfo(context);
TrueTimeRx.build()
.withConnectionTimeout(NTP_CONNECTION_TIMEOUT_IN_MS)
.withRetryCount(50)
.withSharedPreferences(context)
.withLoggingEnabled(BuildConfig.DEBUG)
.initializeRx("time.google.com")
.subscribeOn(Schedulers.io())
.observeOn(AndroidSchedulers.mainThread())
.subscribe(new Subscriber<Date>() {
@Override
public void onCompleted() {
}
@Override
public void onError(Throwable e) {
Crashlytics.logException(e);
}
@Override
public void onNext(Date date) {
Log.d("truetime", "successfully synced time: " + date.toString());
}
});
}
public static synchronized Date getDateTime() {
if (TrueTime.isInitialized()) {
return TrueTime.now();
}
return new Date();
}
from truetime-android.
Caching doesn't work across phone restarts, both on TrueTime and TrueTimeRx versions. I'm using version 3.3
My simple code:
TrueTime.build().withSharedPreferences(context);
if (TrueTime.isInitialized()) {
// do smtn
} else{
TrueTime.build().initialize()
}
from truetime-android.
@nur4i How can it "not work" if that's not even a feature of this library?
from truetime-android.
@EdwardvanRaak Sorry, my bad, didn't fully understand features
from truetime-android.
@nur4i a reboot is explicitly one of those times that TrueTime should be "reset". Have a look at this part of the readme.
@nizamsp @IonutNegru87 : we have a PR #62 that another contributor is helping with. Once they have that PR merged in, i intend to take a more exhaustive look at this again. stay tuned.
from truetime-android.
so the caching changes are in version 3.4
, that being said there's an edge case that still hasn't been accounted for. This is documented here -> #85
closing this issue out in favor of that one. let me know if we're missing anything
from truetime-android.
Related Issues (20)
- Why do we need to put TrueTime initialization inside of the class that extends application?
- How can use list of NTP servers? HOT 1
- DiskCacheClient.isTrueTimeCachedFromAPreviousBoot boot time change check flaw HOT 1
- untrusted stratum value for TrueTime: 0 ? HOT 1
- This library is not mantained any longer! HOT 2
- Fatal Exception: io.reactivex.exceptions.UndeliverableException in version 3.4 HOT 7
- The exception could not be delivered to the consumer because it has already canceled/disposed the flow or the exception has nowhere to go to begin with [SntpClient.java line 116] HOT 1
- It is possible to get synced clock with difference of 10ms maximum?
- It is possible to get synced clock with difference of 10ms maximum?
- dd
- Vulnerable exported broadcast receiver HOT 1
- Outdated Versions of NTP Leaving Users Vulnerable - NTP v3 HOT 7
- Request: Upgrade truetime to work with Android 12 HOT 8
- RxJava3 support HOT 2
- Error : You need to call init() on TrueTime at least once. HOT 7
- TrueTime init() got crash HOT 1
- Add GPS time provider
- Provide default cache implementation using DataStore
- Hi HOT 1
- Exception thrown on Scheduler.Worker thread. Add `onError` handling
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from truetime-android.