'Is it safe to use DataStore in following scenario?
In this example I need to use DataStore inside ViewModel methods
class MyViewModel() : ViewModel() {
suspend fun foo(context: Context): Foo {
val id = context.dataStore.read("key")
return Foo(id = id)
}
}
And also I need to use DataStore inside FirebaseMessagingService() class method
class Foo : FirebaseMessagingService(), CoroutineScope {
override fun onMessageReceived(message: RemoteMessage) {
launch {
//......
val id = this.dataStore.read("key")
}
}
val Context.dataStore: DataStore<Preferences> by preferencesDataStore(name = DEFAULT_DATA_STORE)
suspend fun DataStore<Preferences>.read(key: String): String? {
val dsKey = stringPreferencesKey(key)
return data.firstOrNull()?.get(dsKey)
}
I'm passing contex as parameter
Is it safe doing this approach? Will a memory leak or something occurs?
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|
