Skip to content

Instantly share code, notes, and snippets.

@BapNesS
Created June 9, 2021 17:24
Show Gist options
  • Save BapNesS/3d237c5e95112bffee6173890ae0207a to your computer and use it in GitHub Desktop.
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/
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