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

loloof64

12/25/2021, 5:11 PM
Hi ! I need a way to launch a simple check at each recomposition of my
@Composable
. So I'm using
SideEffect
, but it is not run at each screen update : so is there a better way ?
Copy code
@Composable
fun MyComposable() {
   SideEffect { checkDraw()}
   MySubComposable()
}
a

Adam Powell

12/25/2021, 5:44 PM
What are you trying to do and why?
l

loloof64

12/25/2021, 6:05 PM
It's a kind of complex operation : • I have the main logic in the ViewModel attached to the page of this composable -> it will cause the composable to recompose. • I need to launch a notification if checkDraw() succeed in the Composable, each time it is recomposed : checkDraw will launch the notification which could not be launched from the logic in the ViewModel, if necessary. Hence the SideEffect
It appears in fact that
SideEffect
is always run, but it is a problem in my
MutableStateFlow
. First, it returns the correct state, then an incorrect state is following. What I thought was a problem with
SideEffect
is in fact a problem with my
StateFlow
.
c

Colton Idle

12/27/2021, 2:56 PM
Having a side-effect run every time there is a recomposition sounds really inefficient. Can you model this by just reacting to some state change?
l

loloof64

12/30/2021, 5:03 PM
Yes, indeed, some days ago (I was not at home), I managed to implement my feature without needing
SideEffect
. I mean, I had to think again about the state I'm creating in my
@Composable
.
c

Colton Idle

12/30/2021, 5:07 PM
nice. yeah the new arch docs (updated a few days ago) really help push you in that right mindset about state. def recomend reading it if you haven't already
l

loloof64

12/31/2021, 9:30 AM
Yes thank you. I've read the documentation and created Repository, UseCase and ViewModel in order to handle the complexity of my application 😃