https://kotlinlang.org logo
#coroutines
Title
# coroutines
l

louiscad

02/10/2018, 3:21 PM
I know I can make a full coroutines Activity where the
onCreate(…)
would be a
suspend fun
so I can right away use
withContext(IO)
for things like SharedPreferences access, but should I? Should I use coroutines everywhere so whenever I need to access even small things in storage that may be cached in memory, I can just do it in a non UI thread coroutineContext and suspend when I can?
e

elizarov

02/10/2018, 4:13 PM
Can you elaborate? Maybe some examples?
s

streetsofboston

02/10/2018, 5:05 PM
SharedPreferences is not a good example. These can be read and written on the UI thread, as long as you don't call
commit
on the
SharedPreferences.Editor
.
l

louiscad

02/10/2018, 5:11 PM
@streetsofboston Not exactly: They can be read and written on the UI thread after they've been loaded the first time, but the first load blocks until the underlying xml file has been read from storage and parsed.
s

streetsofboston

02/10/2018, 5:41 PM
I could be mistaken, but already existing ones (backing XML fies) are loaded on startup and
apply
will write them on a background thread. If they are loaded on first access, it seems to be very fast... I've never experienced an issue with it :)
l

louiscad

02/10/2018, 6:46 PM
@streetsofboston I'm currently using the same approach as you, lazy loading the preferences (which usually happens on the UI thread, when they're first used), but I'm aiming for a zero dropped frames app, so I'm attempting to remove absolutely all I/O on UI thread (except the loading of the app binary itself by the system).
👍 2
3 Views