diff --git a/android-client-sdk/src/main/java/com/devcycle/sdk/android/api/DevCycleClient.kt b/android-client-sdk/src/main/java/com/devcycle/sdk/android/api/DevCycleClient.kt index 94a99aa9..200b3ece 100644 --- a/android-client-sdk/src/main/java/com/devcycle/sdk/android/api/DevCycleClient.kt +++ b/android-client-sdk/src/main/java/com/devcycle/sdk/android/api/DevCycleClient.kt @@ -78,13 +78,7 @@ class DevCycleClient private constructor( private val variableInstanceMap: MutableMap>>> = mutableMapOf() init { - val cachedConfig = if (disableConfigCache) null else dvcSharedPrefs.getConfig(user) - if (cachedConfig != null) { - config = cachedConfig - isConfigCached.set(true) - DevCycleLogger.d("Loaded config from cache") - observable.configUpdated(config) - } + useCachedConfigForUser(user) initializeJob = coroutineScope.async(coroutineContext) { isExecuting.set(true) @@ -232,6 +226,10 @@ class DevCycleClient private constructor( fetchConfig(updatedUser) config?.variables?.let { callback?.onSuccess(it) } } catch (t: Throwable) { + DevCycleLogger.d("Error fetching config for user_id %s", updatedUser.userId) + // In the event that the config request fails (i.e. the device is offline) + // Fallback to using a Cached Configuration for the User if available + useCachedConfigForUser(updatedUser) callback?.onError(t) } finally { handleQueuedConfigRequests() @@ -555,6 +553,16 @@ class DevCycleClient private constructor( } } + private fun useCachedConfigForUser(user: PopulatedUser) { + val cachedConfig = if (disableConfigCache) null else dvcSharedPrefs.getConfig(user) + if (cachedConfig != null) { + config = cachedConfig + isConfigCached.set(true) + DevCycleLogger.d("Loaded config from cache for user_id %s", user.userId) + observable.configUpdated(config) + } + } + class DevCycleClientBuilder { private var context: Context? = null private var customLifecycleHandler: Handler? = null