Is it necessarily better or "safer" to call shared...
# android
p
Is it necessarily better or "safer" to call shared prefs from an IO dispatcher?
f
are you talking about the legacy shared prefs? not the data store?
for the legacy shared prefs you can use
apply
instead of
commit
which runs in a background thread
p
Exactly
For example, is it more performative to use
Copy code
withContext(<http://Dispatchers.IO|Dispatchers.IO>){
   sharedPrefsRepo.getSomePref()
}
than to simply call sharedPrefsRepo.getSomePref() synchronously?
I'm assuming yes because the synchronous call blocks the UI thread, but I'm not actually sure
b
It depends on when you call it. When a shared pref object is first created, it loads all the data to memory asynchronously, but will block any getter methods and edit until the data is loaded. Once the data is loaded, all reads are done from an underlying hashmap.