https://kotlinlang.org logo
#android
Title
# android
a

Akhil Jain

12/06/2023, 1:06 PM
Hello everyone, I was working on some optimizations and found that data store is taking significant time in storing/removing values. So why dataStore is slower than shared preference. ?
not kotlin but kotlin colored 3
m

marlonlom

12/06/2023, 1:13 PM
Does that happen when using flow for the storing/removing values?
a

Akhil Jain

12/06/2023, 1:18 PM
Copy code
dataStoreManager.getDataStore(prefName, prefAlreadyExists).edit {
    it.remove(preferencesKey<T>(prefKey))
}
suspend fun <T> DataStore<Preferences>.storeValue(key: Preferences.Key<T>, value: T) {
    this.edit {
        it[key] = value
    }
}
We are using above code for storing/removing
m

marlonlom

12/06/2023, 1:27 PM
ah, where are you using the datastore manager? in the function that uses the suspend function for storing, try to execute it inside a scope block, for example: Viewmodel:
viewModelScope.launch { datastoreManager.storeValue(...) }
Repository:
/* inside a suspend function or flow { ... } */
datastoreManager.storeValue(...)
3 Views