I'm trying to determine whether it's appropriate t...
# compose
d
I'm trying to determine whether it's appropriate to annotate a type I've defined with
@Stable
. The docs say:
Copy code
When applied to a class or an interface, `Stable` indicates that the following must be true:
1. The result of `equals` will always return the same result for the same two instances.
2. When a public property of the type changes, composition will be notified.
3. All public property types are stable.
• For #1, I'm familiar with how
equals
works and can answer this. • Regarding #2, I'm less sure, because I don't know how notification works under the hood. Is it safe to assume that if a public property changes in such a way that using
equals
would evaluate
false
when comparing the before and after, then that means composition will always be notified about the change?
m
i’m hardly an expert, but my assumption would be that if your type is immutable, no public properties can change, so 2 holds, and if your type has some public mutable property that is ignored by
equals
, it needs to be backed by a
MutableState
for 2 to hold
presumably there are ways other than
MutableState
to tie into the composition notification system, but that’s the most common as far as i know
d
@Melody Horn thanks for the reply. And if every property is not ignored by
equals
, then can I assume #2 is met?
j
No. The system needs to know when to even call equals which is what the notification mechanism internal to MutableState provides.
d
@jw I see. So every property in the type needs to be a MutableState (or possibly some other Compose-based item). Thank you!
j
It needs to be backed by
MutableState
, not necessary
MutableState
itself.
👍 1
m
if every property is used in
equals
then 1 means your class needs to be fully immutable
👍 1
z
Technically it needs to be backed by a
StateObject
, not necessarily
MutableState
(eg
SnapshotStateList
is the former not the latter)
If you really want to dig in, more info here
👀 1
thank you color 2
d
oh, nice - I'll give that a read!
c
FWIW, and I know I'm no JW or ZK but if it helps... I've helped ship over 10 apps using compose to production. Ranging from 1M users to 100M on the play store. I have used the @Stable annotation exactly 0 times 😅 There's probably valid use cases for it, but I try to keep my composables simple and follow jim sprochs best practices for compose and weve never had issues. my approach of not worrying about stability unless a measurable issue arises was further validated when compose strong skipping mode was unveiled https://developer.android.com/develop/ui/compose/performance/stability/strongskipping
🔝 1
z
yea unless you're measuring and have proof that instability is causing performance issues, no point
1
111 Views