Question regarding coroutines vs. Android lifecycl...
# android
m
Question regarding coroutines vs. Android lifecycle: Saving to & loading data from a database should not happen on the main thread, hence using coroutines are great for that. Now let’s say I have an activity and save data in
onPause
or
onStop
, e.g. in case the user switches to another application. The launched coroutine’s scope however cannot be bound to the activity’s lifecycle because the save operation may not have finished by the time the activity gets destroyed. Even worse the whole app may be destroyed or hibernated because there are no activities left, which would abort the save operation. How would I model that properly? • The most simple solution I can think of is to block the main thread on
onStop
and wait for the coroutine to complete. But that’s somewhat counter-productive 🤔 OTOH that would ensure that if the user switches to another activity they’d not risk seeing stale data because the save hasn’t completed yet. • Another solution could be a long-running service for any kind of database I/O but that seems unnecessarily complex (and may even need a foreground service?).
i
Assuming you are using ViewModels you can try new version of
AndroidX Lifecycle
library (
2.1.0-rc01
) that provides
viewModelScope
for each ViewModel eg. https://github.com/igorwojda/android-showcase/blob/master/feature_album/src/main/java/com/igorwojda/showcase/feature/album/presentation/albumlist/AlbumListViewModel.kt
l
The process can always get killed without prior notice or timeframe to save data, so you should save data and work to do ASAP. If it's work that could be retried, I'd use WorkManager.
1
👍 2
g
Foreground service for db operation is huge overkill, but any kind application level repository is way to do that
1
m
@igor.wojda the scope is exactly my problem. It would cancel the coroutine saving the data when leaving the activity, which I’m trying to avoid. @louiscad I want to save the work asap. But then according to best practices that shouldn’t happen on the main thread. That means I’d have to dispatch to the background to save the data. Meanwhile the activity gets destroyed because the main thread isn’t blocked. To my understanding that could result in app termination while the background work is still running. @gildor do you mean using a foreground service to keep the app alive while the app repo is still storing data, then stop the service once all pending work is done?
l
@Marc Knaup WorkManager can do its work immediately if you tell it to.
m
@louiscad the documentation for WorkManager tells me otherwise though:
WorkManager is not intended for in-process background work that can safely be terminated if the app process goes away or for tasks that require immediate execution.
(emphasis mine)
l
You can wrap your data storing operation with
withContext(NonCancellable) { … }
.
That will leak the coroutine until the block finishes its execution (which is not a problem if it's quick).
m
That’s good as it would allow using the ViewModel’s scope. The “quick” part is still my concern. I don’t know how long after leaving the application Android would keep the process alive if there is no work queued on the main thread. And under what circumstances. I’m just guessing that in most cases the application won’t be killed until the system needs memory.
g
which is not a problem if it’s quick
Exactly, it’s a hack that may work for sure, but one day may cause problems. So repository solves this issue
l
First case where your app will be killed: Play Store installing an update. You can't rely on observations.
g
do you mean using a foreground service to keep the app alive while the app repo is still storing data
Yes, I think foreground service is really nasty component and not suitable for this case
i
@Marc Knaup why exactly you don’t want to kill this background task in the first place?
m
@gildor why is a foreground service nasty? Isn’t its purpose to keep the app alive while an operation is still pending - exactly what my use case actually is? 🙂
@igor.wojda well because I’d lose data if I cancel it 🤔 The whole point of the task is to store something in the DB upon leaving the activity.
g
Because Foreground Service API is terrible, and because of this: https://issuetracker.google.com/issues/76112072
1
☝️ 1
i
Does this means that your saving operation lasts very long? (In most cases I have seen saving data to DB was very fast, much faster that potential time to kill activity, so there was not risk of actually killing the app and loosing the data)
g
of course it depends on case, we use Foreground Service for some cases, but I would like do not use it
and wouldn’t use it for db operation which in worst case will be a couple secods
m
@igor.wojda it is likely very fast. But I wouldn’t rely on even a millisecond of time available for background operations after the activity has been destroyed unless it’s documented somewhere that the app is guaranteed some time (except for edge cases like app updates and OOM).
g
app is guaranteed some time
there is no such thing as guaranteed time
m
@gildor with a foreground service I’d be guaranteed the opportunity to do some work. Only edge cases would kill the app, like OOM, app update, user kill - all of which are fine.
g
I believe your problem is original requirement “Now let’s say I have an activity and save data in
onPause
or `onStop`“, would be much better save when something change, not onStop
Only edge cases
…or ANR that you cannot avoid because forground service is starting longer than required by startForegroundService() (see issue above)
i
IMO most project have this assumption that Presenter/ViewModel lives long enough to save data to Data layer (Repository / DB) (although it’s never 100% guaranteed, it’s externally rare to actually happen - I mean kill activity before finishing ms write that lasts few ms)
1
g
I mean kill activity before finishing few ms write
Which is even more rare if you just save on user action (user input, save button etc) instead of waiting onStop
👍 1
m
@gildor saving on user action would change the actual use case. The user would overwrite data he may not wish to overwrite. At the very end when leaving the activity they may just discard the changes, which then isn’t possible anymore.
I believe your problem is original requirement “Now let’s say I have an activity and save data in
onPause
or `onStop`“, would be much better save when something change, not onStop
g
The user would overwrite data he may not wish to overwrite
why so? How is that different from saving while onStop?
m
You’re right, I’m mixing up two different Activities I have with different behavior when they save and when not. So let me focus on the one which needs to persist data upon stopping (and thus could do so at any time before stopping). If I’d save on each change then I’d have to save after every single keystroke. The operation would be relatively expensive because it involves JSON serialization. Maybe I could delay saving if the last save was less than X milliseconds ago (similar to debounce in RX). Unless it’s onStop in which case I’d save immediately again. 🤔
g
If I’d save on each change then I’d have to save after every single keystroke
just use debounce()
m
Refactoring for removing RX is what I’m currently working on, so I have no debounce 😄
g
and what do you have? and why do you removing Rx?
m
LiveData & coroutines
g
So use Flow.debounce() 🤷‍♂️
☝️ 1
1
m
Because RX made our project incredibly complex, difficult to understand and maintain - way more than it needs to be.
g
Replace Rx with LiveData is quite strange 😬
m
Never been a fan of it and likely never will be
Well, it was also strange what RX was used for in the project 😛
At some point it because quite difficult to understand where data is actually flowing. And Exception reporting was basically useless.
Haven’t used Flow yet but I guess I’ll give it a try soon.
g
anyway, it’s offtopic, you free to use Flow or write own extension to debounce changes if you don’t want to use existing one
At some point it because quite difficult to understand where data is actually flowing.
And Exception reporting was basically useless.
and how would it be easier without Rx?
m
I would actually get a stack trace rather than “no onError has been registered”
g
I mean for sure, sequential asyncronous code with Single/Maybe/Completable is much-much more readable with coroutines suspend functions, but if you need stream of events, I don’t see how such thing as LiveData may be better
no onError has been registered
This can be handled by global Rx error handler, and also, why you don’t have onError?
m
I try to stay outside of LiveData whatever I can 🙂 @gildor ask the developer from who I took over the project 🤷‍♂️ Even button clicks were RX events.
g
anyway it’s offtopic for sure, we need a couple of beers and a cosy bar to discuss this properly 🙈
🙂 1
i
I haven;t tried flow as well, but you can also use this simple trick for debounce
Copy code
var quoteJob: Job? = null
...
fun saveData(data) {
    quoteJob?.cancel()
    quoteJob = CoroutineScope(<http://coroutineContentProvider.IO|coroutineContentProvider.IO>).launch {
        delay(200)
        db.saveData(data)
    }
}
m
@igor.wojda that’s not debounce though. It keeps delaying 200ms after every update, not 200ms after the first update since the last save.
g
yes, it will work we use it in many cases, quite often for differenrent repositories
m
So if there is an update very 199ms it would basically never save?
g
this is exactly how Rx or Flow debounce() works
anyway, sample() also works for this, depends on use case
m
ugh, then the developer didn’t even implement that correctly 🙄 Anyway, thanks for the help. I have a lot more input now I can work with 🙂
i
There is some confusion regarding names as I recall between
throttling
and
debouncing
From my notes:
debouncing
- Combine bunch a series of sequential calls to a function into a single call to that function. It ensures that one notification is made for an event that fires multiple times e.g. button click
throttling
- Delay executing a function. It will reduce the notifications of an event that fires multiple times e.g. window.scroll, window resize
👍 1
m
Yes, throttle seems to be what was supposed to be used in the first place 😅
g
There are 3 different throttle strategies (actually 4, just checked)
👍 1
you probaby want sample/throttleLast
m
Anyway, moving away from RX. I’ll think about how to implement it with coroutines or maybe Flow.
g
Just to be clear, Flow is essentially the same Rx, but with coroutines
Flow also have debounce() and sample() operators
m
Yeah. I haven’t used Flow yet and probably won’t add it to the project just for a few cases.
g
you already have it in your project if use kotlinx.coroutines
m
oh, is it out of experimental?
g
not yet, but will be stable relatevely soon I believe
👍 1
l
It's out of experimental already in 1.3.0-RC2. Some/many operators are still experimental however. And 1.3.0 is very near, probably coming this week with very few changes, if any.
m
cool 🙂 Will need it for my mongodb driver coroutine wrapper.
l
Flow is still simpler than Rx. Starting to use Flow when you know coroutines is very easy in my experience, and handling back-pressure if needed is a no brainer.
g
Flow is still simpler than Rx
Simpler or aka “not all Rx operators implemented… yet”
😂 3
but alredy much closer to it
g
anyway, I agree, Kotlin and nice design make it easier to use in many cases, but I would say it’s more about nuances than about some dramatic difference
m
The backpressure part is where it’s a lot simpler as far as I read.
g
writing custom operators or write custom Flow builders is much easier with Flow, for sure, but it needed mostly for relatevely advanced cases (tho, it’s very nice thing)
don’t see any problems with Rx backpressure too (esepecially in RxJava)
but thanks to coroutines implementation of backpressure is a lot easier
a
If we back to original question. WorkManager is better than service - that for sure. Blocking main thread is not good idea, but saving small Json - should not take to match time. So
runBlocking
for some
saveData()
method in
onStop
should be just fine. BTW - you should be able to save all your data while the viewmodel is active. All user inputs - could be persisted using onSave/onRestore instance state.
👎 1
l
@ahulyk
runBlocking
on the main thread is never fine. You're risking your app to be killed because of an ANR if the storage is busy (e.g. while the Play Store installs an update on a device with slow storage, i.e. most cheap devices).
saved instance state is not blocking and safe to use on main thread because the Android OS saves the data in memory, but that's not the case for custom I/O.
m
@ahulyk it’s actually JSON serialization + multiple SQLite writes 🙂 IO on the main thread is always discouraged and for a good reason. Best is to not make assumptions about IO performance.
l
You can make assumptions on I/O performance to be slow I'd say. Make the assumption every storage I/O could take more than 5 seconds (i.e. past the ANR deadline, and way past user patience), even for just saving a single byte.
m
Yup, good way to put it 🙂