https://kotlinlang.org logo
Channels
100daysofcode
100daysofkotlin
100daysofkotlin-2021
advent-of-code
aem
ai
alexa
algeria
algolialibraries
amsterdam
android
android-architecture
android-databinding
android-studio
androidgithubprojects
androidthings
androidx
androidx-xprocessing
anime
anko
announcements
apollo-kotlin
appintro
arabic
argentina
arkenv
arksemdevteam
armenia
arrow
arrow-contributors
arrow-meta
ass
atlanta
atm17
atrium
austin
australia
austria
awesome-kotlin
ballast
bangladesh
barcelona
bayarea
bazel
beepiz-libraries
belgium
benchmarks
berlin
big-data
books
boston
brazil
brikk
budapest
build
build-tools
bulgaria
bydgoszcz
cambodia
canada
carrat
carrat-dev
carrat-feed
chicago
chile
china
chucker
cincinnati-user-group
cli
clikt
cloudfoundry
cn
cobalt
code-coverage
codeforces
codemash-precompiler
codereview
codingame
codingconventions
coimbatore
collaborations
colombia
colorado
communities
competitive-programming
competitivecoding
compiler
compose
compose-android
compose-desktop
compose-hiring
compose-ios
compose-mp
compose-ui-showcase
compose-wear
compose-web
confetti
connect-audit-events
corda
cork
coroutines
couchbase
coursera
croatia
cryptography
cscenter-course-2016
cucumber-bdd
cyprus
czech
dagger
data2viz
databinding
datascience
dckotlin
debugging
decompose
decouple
denmark
deprecated
detekt
detekt-hint
dev-core
dfw
docs-revamped
dokka
domain-driven-design
doodle
dsl
dublin
dutch
eap
eclipse
ecuador
edinburgh
education
effective-kotlin
effectivekotlin
emacs
embedded-kotlin
estatik
event21-community-content
events
exposed
failgood
fb-internal-demo
feed
firebase
flow
fluid-libraries
forkhandles
forum
fosdem
fp-in-kotlin
framework-elide
freenode
french
fritz2
fuchsia
functional
funktionale
gamedev
ge-kotlin
general-advice
georgia
geospatial
german-lang
getting-started
github-workflows-kt
glance
godot-kotlin
google-io
gradle
graphic
graphkool
graphql
graphql-kotlin
graviton-browser
greece
grpc
gsoc
gui
hackathons
hacktoberfest
hamburg
hamkrest
helios
helsinki
hexagon
hibernate
hikari-cp
hire-me
hiring
hongkong
hoplite
http4k
hungary
hyderabad
image-processing
india
indonesia
inkremental
intellij
intellij-plugins
intellij-tricks
internships
introduce-yourself
io
ios
iran
israel
istanbulcoders
italian
jackson-kotlin
jadx
japanese
jasync-sql
java-to-kotlin-refactoring
javadevelopers
javafx
javalin
javascript
jdbi
jhipster-kotlin
jobsworldwide
jpa
jshdq
juul-libraries
jvm-ir-backend-feedback
jxadapter
k2-early-adopters
kaal
kafka
kakao
kalasim
kapt
karachi
karg
karlsruhe
kash_shell
kaskade
kbuild
kdbc
kgen-doc-tools
kgraphql
kinta
klaxon
klock
kloudformation
kmdc
kmm-español
kmongo
knbt
knote
koalaql
koans
kobalt
kobweb
kodein
kodex
kohesive
koin
koin-dev
komapper
kondor-json
kong
kontent
kontributors
korau
korean
korge
korim
korio
korlibs
korte
kotest
kotest-contributors
kotless
kotlick
kotlin-asia
kotlin-beam
kotlin-by-example
kotlin-csv
kotlin-data-storage
kotlin-foundation
kotlin-fuel
kotlin-in-action
kotlin-inject
kotlin-latam
kotlin-logging
kotlin-multiplatform-contest
kotlin-mumbai
kotlin-native
kotlin-pakistan
kotlin-plugin
kotlin-pune
kotlin-roadmap
kotlin-samples
kotlin-sap
kotlin-serbia
kotlin-spark
kotlin-szeged
kotlin-website
kotlinacademy
kotlinbot
kotlinconf
kotlindl
kotlinforbeginners
kotlingforbeginners
kotlinlondon
kotlinmad
kotlinprogrammers
kotlinsu
kotlintest
kotlintest-devs
kotlintlv
kotlinultimatechallenge
kotlinx-datetime
kotlinx-files
kotlinx-html
kotrix
kotson
kovenant
kprompt
kraph
krawler
kroto-plus
ksp
ktcc
ktfmt
ktlint
ktor
ktp
kubed
kug-leads
kug-torino
kvision
kweb
lambdaworld_cadiz
lanark
language-evolution
language-proposals
latvia
leakcanary
leedskotlinusergroup
lets-have-fun
libgdx
libkgd
library-development
lincheck
linkeddata
lithuania
london
losangeles
lottie
love
lychee
macedonia
machinelearningbawas
madrid
malaysia
mathematics
meetkotlin
memes
meta
metro-detroit
mexico
miami
micronaut
minnesota
minutest
mirror
mockk
moko
moldova
monsterpuzzle
montreal
moonbean
morocco
motionlayout
mpapt
mu
multiplatform
mumbai
munich
mvikotlin
mvrx
myndocs-oauth2-server
naming
navigation-architecture-component
nepal
new-mexico
new-zealand
newname
nigeria
nodejs
norway
npm-publish
nyc
oceania
ohio-kotlin-users
oldenburg
oolong
opensource
orbit-mvi
osgi
otpisani
package-search
pakistan
panamá
pattern-matching
pbandk
pdx
peru
philippines
phoenix
pinoy
pocketgitclient
polish
popkorn
portugal
practical-functional-programming
proguard
prozis-android-backup
pyhsikal
python
python-contributors
quasar
random
re
react
reaktive
realm
realworldkotlin
reductor
reduks
redux
redux-kotlin
refactoring-to-kotlin
reflect
refreshversions
reports
result
rethink
revolver
rhein-main
rocksdb
romania
room
rpi-pico
rsocket
russian
russian_feed
russian-kotlinasfirst
rx
rxjava
san-diego
science
scotland
scrcast
scrimage
script
scripting
seattle
serialization
server
sg-user-group
singapore
skia-wasm-interop-temp
skrape-it
slovak
snake
sofl-user-group
southafrica
spacemacs
spain
spanish
speaking
spek
spin
splitties
spotify-mobius
spring
spring-security
squarelibraries
stackoverflow
stacks
stayhungrystayfoolish
stdlib
stlouis
strife-discord-lib
strikt
students
stuttgart
sudan
swagger-gradle-codegen
swarm
sweden
swing
swiss-user-group
switzerland
talking-kotlin
tallinn
tampa
teamcity
tegal
tempe
tensorflow
terminal
test
testing
testtestest
texas
tgbotapi
thailand
tornadofx
touchlab-tools
training
tricity-kotlin-user-group
trójmiasto
truth
tunisia
turkey
turkiye
twitter-feed
uae
udacityindia
uk
ukrainian
uniflow
unkonf
uruguay
utah
uuid
vancouver
vankotlin
vertx
videos
vienna
vietnam
vim
vkug
vuejs
web-mpp
webassembly
webrtc
wimix_sentry
wwdc
zircon
Powered by
Title
m

Marc Knaup

08/13/2019, 12:15 PM
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

igor.wojda

08/13/2019, 12:51 PM
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

louiscad

08/13/2019, 12:56 PM
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

gildor

08/13/2019, 2:10 PM
Foreground service for db operation is huge overkill, but any kind application level repository is way to do that
1
m

Marc Knaup

08/13/2019, 2:16 PM
@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

louiscad

08/13/2019, 2:17 PM
@Marc Knaup WorkManager can do its work immediately if you tell it to.
m

Marc Knaup

08/13/2019, 2:21 PM
@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

louiscad

08/13/2019, 2:23 PM
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

Marc Knaup

08/13/2019, 2:25 PM
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

gildor

08/13/2019, 2:30 PM
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

louiscad

08/13/2019, 2:30 PM
First case where your app will be killed: Play Store installing an update. You can't rely on observations.
g

gildor

08/13/2019, 2:31 PM
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

igor.wojda

08/13/2019, 2:31 PM
@Marc Knaup why exactly you don’t want to kill this background task in the first place?
m

Marc Knaup

08/13/2019, 2:32 PM
@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

gildor

08/13/2019, 2:35 PM
Because Foreground Service API is terrible, and because of this: https://issuetracker.google.com/issues/76112072
1
☝️ 1
i

igor.wojda

08/13/2019, 2:35 PM
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

gildor

08/13/2019, 2:35 PM
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

Marc Knaup

08/13/2019, 2:36 PM
@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

gildor

08/13/2019, 2:36 PM
app is guaranteed some time
there is no such thing as guaranteed time
m

Marc Knaup

08/13/2019, 2:37 PM
@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

gildor

08/13/2019, 2:38 PM
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

igor.wojda

08/13/2019, 2:39 PM
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

gildor

08/13/2019, 2:40 PM
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

Marc Knaup

08/13/2019, 2:41 PM
@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

gildor

08/13/2019, 2:42 PM
The user would overwrite data he may not wish to overwrite
why so? How is that different from saving while onStop?
m

Marc Knaup

08/13/2019, 2:52 PM
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

gildor

08/13/2019, 2:52 PM
If I’d save on each change then I’d have to save after every single keystroke
just use debounce()
m

Marc Knaup

08/13/2019, 2:53 PM
Refactoring for removing RX is what I’m currently working on, so I have no debounce 😄
g

gildor

08/13/2019, 2:53 PM
and what do you have? and why do you removing Rx?
m

Marc Knaup

08/13/2019, 2:53 PM
LiveData & coroutines
g

gildor

08/13/2019, 2:53 PM
So use Flow.debounce() 🤷‍♂️
☝️ 1
1
m

Marc Knaup

08/13/2019, 2:54 PM
Because RX made our project incredibly complex, difficult to understand and maintain - way more than it needs to be.
g

gildor

08/13/2019, 2:54 PM
Replace Rx with LiveData is quite strange 😬
m

Marc Knaup

08/13/2019, 2:54 PM
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

gildor

08/13/2019, 2:56 PM
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

Marc Knaup

08/13/2019, 2:57 PM
I would actually get a stack trace rather than “no onError has been registered”
g

gildor

08/13/2019, 2:57 PM
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

Marc Knaup

08/13/2019, 2:59 PM
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

gildor

08/13/2019, 2:59 PM
anyway it’s offtopic for sure, we need a couple of beers and a cosy bar to discuss this properly 🙈
🙂 1
i

igor.wojda

08/13/2019, 2:59 PM
I haven;t tried flow as well, but you can also use this simple trick for debounce
var quoteJob: Job? = null
...
fun saveData(data) {
    quoteJob?.cancel()
    quoteJob = CoroutineScope(<http://coroutineContentProvider.IO|coroutineContentProvider.IO>).launch {
        delay(200)
        db.saveData(data)
    }
}
m

Marc Knaup

08/13/2019, 3:00 PM
@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

gildor

08/13/2019, 3:00 PM
yes, it will work we use it in many cases, quite often for differenrent repositories
m

Marc Knaup

08/13/2019, 3:01 PM
So if there is an update very 199ms it would basically never save?
g

gildor

08/13/2019, 3:01 PM
this is exactly how Rx or Flow debounce() works
anyway, sample() also works for this, depends on use case
m

Marc Knaup

08/13/2019, 3:03 PM
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

igor.wojda

08/13/2019, 3:04 PM
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

Marc Knaup

08/13/2019, 3:04 PM
Yes, throttle seems to be what was supposed to be used in the first place 😅
g

gildor

08/13/2019, 3:05 PM
There are 3 different throttle strategies (actually 4, just checked)
👍 1
you probaby want sample/throttleLast
m

Marc Knaup

08/13/2019, 3:08 PM
Anyway, moving away from RX. I’ll think about how to implement it with coroutines or maybe Flow.
g

gildor

08/13/2019, 3:08 PM
Just to be clear, Flow is essentially the same Rx, but with coroutines
Flow also have debounce() and sample() operators
m

Marc Knaup

08/13/2019, 3:09 PM
Yeah. I haven’t used Flow yet and probably won’t add it to the project just for a few cases.
g

gildor

08/13/2019, 3:10 PM
you already have it in your project if use kotlinx.coroutines
m

Marc Knaup

08/13/2019, 3:11 PM
oh, is it out of experimental?
g

gildor

08/13/2019, 3:11 PM
not yet, but will be stable relatevely soon I believe
👍 1
l

louiscad

08/13/2019, 3:13 PM
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

Marc Knaup

08/13/2019, 3:14 PM
cool 🙂 Will need it for my mongodb driver coroutine wrapper.
l

louiscad

08/13/2019, 3:14 PM
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

gildor

08/13/2019, 3:18 PM
Flow is still simpler than Rx
Simpler or aka “not all Rx operators implemented… yet”
😂 3
but alredy much closer to it
g

gildor

08/13/2019, 3:19 PM
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

Marc Knaup

08/13/2019, 3:20 PM
The backpressure part is where it’s a lot simpler as far as I read.
g

gildor

08/13/2019, 3:21 PM
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

ahulyk

08/14/2019, 3:52 PM
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

louiscad

08/14/2019, 3:59 PM
@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

Marc Knaup

08/14/2019, 4:00 PM
@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

louiscad

08/14/2019, 4:02 PM
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

Marc Knaup

08/14/2019, 4:03 PM
Yup, good way to put it 🙂