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
hiring-french
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
u

ursus

01/26/2020, 4:48 PM
Is there a way it can collapse handler.posts so close together somehow? cc @Adam Powell
a

Adam Powell

01/26/2020, 4:54 PM
no, the semantics of specific posted handler messages are opaque to the handler mechanism itself. You're describing a higher level policy that handlers shouldn't know about.
u

ursus

01/26/2020, 5:07 PM
I see. Just to clarify, it doesnt make any sense to update views 1ms or whatever apart (i.e. shorter than vsync), and therefore
stateFlow
should be debounced to vsync value at app level? (or is this debounces somehow at a lower layer)
a

Adam Powell

01/26/2020, 5:44 PM
most view properties boil down to internal property sets and a call to invalidate or requestLayout as appropriate, the latter two operations are very cheap if a pending draw or layout is already scheduled.
so some debouncing is happening at a higher layer already
TextView#setText has some cost associated with it but not one you're likely to see a lot of in most cases.
Or to put it another way, have you measured a problem with this approach so far? 🙂
u

ursus

01/26/2020, 6:40 PM
I havent measured as its not a perf problem but consistency, but if ui state is actually rendered only on vsync then it should be okay, althought I set it twice without debounce
whole issue basically is if written to room it will emit later than suspend function which writes it, returns
tldr; writing to db is sideffect, and that messes up the whole flow
viewModel.scope {
   room.itemFlow
    .collect {
        itemLiveData.set = it
    }
}
viewModel.scope {
   room.updateItem
   updateCompleteLiveData.set = true
}
here updateCompleteLiveData will be true, but the itemLiveData will still contain item where item.activated == false for a brief moment
a

Adam Powell

01/26/2020, 7:37 PM
I'm pretty sure you can make room updates use suspend calls so that you know the update is complete before you move on
u

ursus

01/26/2020, 7:52 PM
sure, but the room query flow will emit whenever it will emit, regardless if update is suspending or not
you might notice that in the logs of the original question
a

Adam Powell

01/26/2020, 8:03 PM
so if I'm reading this right, you have a LiveData reflecting the activated state, but it's set by a process that is independent of the actual database change being reflected?
u

ursus

01/26/2020, 8:08 PM
not exactly, its just an example, imagine the items live data contains items (room query stream), and activation live data contain# events as such to show toast "activation successful" after
suspend fun activate(itemId) {
   api.activate(itemId)
   db.setActivated(itemId)
}
...
viewModelScope.launch {
   activate(itemId)
   activationLiveData.set = ActivationState.Success
}
...
viewModelScope.launch {
   db.itemById(...).asFlow().collect {
     itemLiveData.set = it
   }
}
something like this, whereas Fragment reacts to activationLiveData by showing toasts for example, and to itemLiveData refeshing the recycler view or somethimg
issue is: there is point in time when itemLiveData contains item which item.activated == false, while activationLiveData already contains success
a

Adam Powell

01/26/2020, 8:12 PM
ok, it still seems a bit like there's some sort of a multiple sources of truth issue at play, or a question of what data is being modeled. If the activation state is something stored in the database, the LiveData for the activation state of a single item could just as well be
roomTableState.mapNotNull { it.firstOrNull { it.itemId == itemId }?.isActivated }
or whatnot, right?
er, pretend I tacked a,
.asLiveData()
on the end of that 🙂
u

ursus

01/26/2020, 8:15 PM
well exactly, its because its 2 sources of truth. the activation live data contains the state of the whole "routine" and its in memory only
however imagine there will be Loading and error states to it as well viewModelScope.launch { activationLiveData.set = Loading activate() activationLiveData.set = Success }
I could model this as a column in the db as you said, but, then I have the burden of clearing the transient states on app start, as they are now persistent, and im not sure if this is so great for every such use case as it is very common,.. or is it?
a

Adam Powell

01/26/2020, 8:17 PM
well, it's more about deciding what observed events/stream items need to have what kind of serialization applied
using the db as the source of truth is one way, another would be to route other reads of db changes through some sort of repository object that muxes together your other in-memory signals
and then zip/combineLatest as appropriate, or what have you
if this idea of, "activated" isn't present in the db itself, then it doesn't much matter if other parts of the system see implementation detail db changes along the way as long as any decisions they might make at any snapshot are valid/consistent
and if those db changes are intended to be implementation details that don't get leaked, you need some sort of filter/serialization construct in front of other things that read it
u

ursus

01/26/2020, 8:22 PM
hmm, in this case I was thinking only certain ui cares about the in flight activation state as to show progressbar / toasts, othere dont
a

Adam Powell

01/26/2020, 8:23 PM
does the content of the stream from your log matter then?
u

ursus

01/26/2020, 8:25 PM
of what stream? I simplfied it here for this convo. Actually I have just 1 livedata/flow and a single State which contains all viewmodel state, and copy construct new instances
a

Adam Powell

01/26/2020, 8:26 PM
you mentioned that the order of observing the db updates from room vs. direct LiveData writes made during different db manipulating operations was bothering you
is it a problem, or is it just a quirk of the data flow?
u

ursus

01/26/2020, 8:28 PM
well, the code above produces 2 State changes, 1ms apart, of which the first is basically bogus, since it contains activation=success, and the item.activated is still false
after the 1ms or whenever io scheduler desires it will emit new db item with activated=true reflected and then its all good
a

Adam Powell

01/26/2020, 8:30 PM
so can the activated liveData be a mapping from the flow of db updates instead, so that there's only ever a single source of truth?
u

ursus

01/26/2020, 8:30 PM
and it botheres me since ui will react to the 1st emit and will try for example crossfade recyclerview with progressbar, and then after 1ms the new item will come and I will refresh thr adapter basically during the animation
a

Adam Powell

01/26/2020, 8:30 PM
You mentioned there were a few other properties not reflected as db columns but this doesn't seem like one of those
u

ursus

01/26/2020, 8:31 PM
no, if the request state is a column, then everything is fine and my question is invalid
a

Adam Powell

01/26/2020, 8:32 PM
oh I see, you're reflecting this as pending vs. committed
u

ursus

01/26/2020, 8:32 PM
however it feels like a overkill, I never seen people persisting the request state of every table in database, also I will need multiple columns per action, imagine actions as activate, rename, doawhatever.., all would need to be a separate column
a

Adam Powell

01/26/2020, 8:34 PM
yeah I wouldn't do that.
u

ursus

01/26/2020, 8:34 PM
however it do would solve my dual truth problem
a

Adam Powell

01/26/2020, 8:34 PM
it does sound like there's some property of, "only the db reflecting activated = true can change the state from whatever => activated" that would be useful to leverage
u

ursus

01/26/2020, 8:35 PM
If I were to combineLatest the 2 streams, would you just debounce it by x, and call it a day?
a

Adam Powell

01/26/2020, 8:36 PM
probably. Even if you got this stream perfectly synchronized you'll still have weird cases of one or two frames between these events and still get some flicker in the UI
IO latency can go way up on some devices under load or on low-end devices with slow flash parts
u

ursus

01/26/2020, 8:37 PM
true, basically what I need is a way to attribute the db query emit to who caused it
since now it anonymous. If I knew that, then I could combineLatest safely
a

Adam Powell

01/26/2020, 8:38 PM
that tends to be quite difficult for a lot of IO systems, not just things backed by sqlite change observers
u

ursus

01/26/2020, 8:39 PM
yea -.-
I could just pull the query not push but then whole modern approach is a bust
a

Adam Powell

01/26/2020, 8:40 PM
but if it were me, I'd probably do like you're saying and just debounce it for now. imo the problem is further downstream with the way that quick, successive changes to the UI have unwanted side effects on the views being used to implement/animate it, which is something we're looking to address with #compose in particular
there have also been a number of other changes to android animation systems to better handle interruption/new targets of the animation
u

ursus

01/26/2020, 8:41 PM
I think ui is just where it shows, the actual issue is not representing invalid state, there still will be transiently invalid state, compose will just make it look good
😂 1
a

Adam Powell

01/26/2020, 8:42 PM
could be, in which case my question from above comes back. If you're representing invalid state between different flow/LD emits, then you'll want to bring those into the same stream and only ever emit valid states that way, without letting anything else side-channel observe your db changes
u

ursus

01/26/2020, 8:42 PM
yes
but how can I tell which is valid at the subscriber level? its not always clear, and also the debounce-and-pray can be broken based on business of io as you said
a

Adam Powell

01/26/2020, 8:47 PM
individual subscribers have to be able to assume anything they see is valid; you'll want to enforce consistency as far upstream as possible. That seems like it's some sort of combine/merge point between your in-flight operations and the db updates themselves
which means you can't permit anything but this code that does this consistency enforcement to hand out a
Flow
of your raw observed db changes
you might take a look at how things like https://github.com/dropbox/Store address cases like this
u

ursus

01/26/2020, 8:51 PM
im pretty sure ive seen a case where subscriber couldnt tell, let me think, sec..
I remember, it was pagination in chat, were user could delete a message
so I couldnt just assume if next emit contains more messages = pagination happened
in this case is not invalid "as much" but there still exists a point in time, where old list is shown without a progressbar at the end (since paginationState==Finished, and db items are still old since io didnt emit yet), which doesnt make sense; and also its a pointless adapter notify call/list diffing just to be interuppted intederminate few ms later -- same issue as we were talking about with the activation thing. And here I cannot determine if pagination happened (at least easily naively, since user can send new messages during the chat history pagination is in flight), and can only debounce-and-pray, so is really "best effort" best reactive programming can do here? Since the pagination library of you guys, internally its basically imperative / blocking listeners if I remember correctly