Created
June 9, 2021 17:24
-
-
Save BapNesS/3d237c5e95112bffee6173890ae0207a to your computer and use it in GitHub Desktop.
Sample for suspend usage of Firebase Remote Config "fetchAndActivate()". Check the full sample for more informations: https://github.com/BapNesS/android-sample-firebaseremoteconfiguration/
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class SyncViewModel : BaseViewModel() { | |
fun loadCacheFirebaseConfig() { | |
firebaseRemoteConfigInstance.apply { | |
RemoteConfigKey.values().forEach { key -> | |
getString(key.name) | |
.takeIf { it.isNotBlank() } | |
?.let { newValue -> | |
val remoteConfigData = RemoteConfigData(key, newValue) | |
LocalConfig.setData(remoteConfigData) | |
} | |
} | |
} | |
} | |
override fun initLoading() { | |
loadCacheFirebaseConfig() | |
viewModelScope.launch { | |
fetchFirebaseConfig() | |
updateDisplay() | |
} | |
} | |
private suspend fun fetchFirebaseConfig() : Boolean = suspendCoroutine { continuation -> | |
firebaseRemoteConfigInstance.fetchAndActivate().addOnCompleteListener { task -> | |
if (task.isSuccessful) { | |
loadCacheFirebaseConfig() | |
} | |
continuation.resume(true) | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment