Wajahat Karim
07/28/2020, 2:04 PMstate
in Compose dev15. So, if create a state like val numState by state { 0 }
, and use this in any composable like Text, it works good and fine. On doing numState++
, the Text updates. But, if I use some custom data class like this below code:
data class Point(val x:Int = 0, val y:Int = 0)
And using this class to create state like val pointState by state { Point(3,4) }
, and now if I update this like pointState.x++
, then the Composables using this state aren't recomposed and updated. I don't understand how am I supposed to use state?Adam Powell
07/28/2020, 2:08 PMWajahat Karim
07/28/2020, 2:10 PMvar newPoint = pointState.copy(x = 3)
and reassiging this to pointState = newPoint
, then it worked. But I am confused on whether this is a good practice, considering if contents are some ArrayLists etc instead of simple x, y integers performance wise?Adam Powell
07/28/2020, 2:12 PMmutableStateListOf
for example creates a list backed by persistent collectionsMutableState
for properties of mutable objects, you do get snapshot isolation for thread safety, plus observability, but it does come with overhead beyond a simple Int
property; we're working to keep it cheap enough to use frequently but it's not free 🙂Wajahat Karim
07/28/2020, 2:15 PMmutableStateListOf
and update it, then this will work?MutableState
last night and will definitely experiment with it today to see if it solves my problem.Adam Powell
07/28/2020, 2:16 PM@Stable
class Point(x: Int, y: Int) {
var x by mutableStateOf(x)
var y by mutableStateOf(y)
}
// usage; note 'remember' instead of 'state'
val point = remember { Point(1, 2) }
than_
07/28/2020, 2:17 PMAdam Powell
07/28/2020, 2:17 PMthan_
07/28/2020, 2:20 PMWajahat Karim
07/28/2020, 2:20 PMAdam Powell
07/28/2020, 2:21 PMawaitFrame
method was renamed to withFrameMillis
iircWajahat Karim
07/28/2020, 2:23 PMawaitFrameMillis
and lifecycleOwner
are unresolved variablesAdam Powell
07/28/2020, 2:26 PMlifecycleOwner
is a local variable, the whenStarted
extension comes from the lifecycle-ktx
libraryLifecycleOwnerAmbient
is still there, should just be an importWajahat Karim
07/28/2020, 2:30 PMawaitFrame
is deprecated in dev15.Adam Powell
07/28/2020, 2:32 PMwithFrameMillis
Wajahat Karim
07/28/2020, 2:33 PM