Skip to content

Commit d412faa

Browse files
committed
Expose isInitialized, try/catch store initialization
1 parent 08327a2 commit d412faa

File tree

3 files changed

+14
-2
lines changed

3 files changed

+14
-2
lines changed

src/main/java/com/statsig/androidsdk/Statsig.kt

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -326,6 +326,16 @@ object Statsig {
326326
return result ?: StatsigOverrides(mutableMapOf(), mutableMapOf())
327327
}
328328

329+
/**
330+
* @return true if the SDK is initialized (usable), though the most up
331+
* to date values will not be fetched from the network until async initialization
332+
* is complete
333+
*/
334+
@JvmStatic
335+
fun isInitialized(): Boolean {
336+
return client.isInitialized()
337+
}
338+
329339
private fun enforceInitialized(functionName: String) {
330340
client.enforceInitialized(functionName)
331341
}

src/main/java/com/statsig/androidsdk/Store.kt

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -44,8 +44,8 @@ internal class Store (private var userID: String?, private var customIDs: Map<St
4444
cacheById = mutableMapOf()
4545

4646
if (cachedResponse != null) {
47-
val type = object : TypeToken<MutableMap<String, Cache>>() {}.type
4847
try {
48+
val type = object : TypeToken<MutableMap<String, Cache>>() {}.type
4949
cacheById = gson.fromJson(cachedResponse, type) ?: cacheById
5050
} catch (_: Exception) {
5151
StatsigUtil.removeFromSharedPrefs(sharedPrefs, CACHE_BY_USER_KEY)
@@ -54,8 +54,8 @@ internal class Store (private var userID: String?, private var customIDs: Map<St
5454

5555
stickyDeviceExperiments = mutableMapOf()
5656
if (cachedDeviceValues != null) {
57-
val type = object : TypeToken<MutableMap<String, APIDynamicConfig>>() {}.type
5857
try {
58+
val type = object : TypeToken<MutableMap<String, APIDynamicConfig>>() {}.type
5959
stickyDeviceExperiments = gson.fromJson(cachedDeviceValues, type) ?: stickyDeviceExperiments
6060
} catch (_: Exception) {
6161
StatsigUtil.removeFromSharedPrefs(sharedPrefs, STICKY_DEVICE_EXPERIMENTS_KEY)

src/test/java/com/statsig/androidsdk/StatsigCacheTest.kt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,9 @@ class StatsigCacheTest {
5555

5656
testSharedPrefs.edit().putString("Statsig.CACHE_BY_USER", gson.toJson(cacheById))
5757

58+
assertFalse(Statsig.isInitialized())
5859
TestUtil.startStatsigAndDontWait(app, user, StatsigOptions())
60+
assertTrue(Statsig.isInitialized())
5961
client = Statsig.client
6062
assertTrue(client.checkGate("always_on"))
6163

0 commit comments

Comments
 (0)