I want to be able to directly invalidate a `rememb...
# compose
y
I want to be able to directly invalidate a
remember
. It seems that
currentComposer.cache
does this, but with a big disclaimer and no-no all over it. Is there any supported way to do this? Would it be okay to do something like this instead:
Copy code
private object Flip {
  override fun equals(other: Any?): Boolean {
    // Not equal to itself
    return Keep == other
  }
}

private data object Keep

remember(if (shouldUpdate) Flip else Keep) { ... }
This works, but I'm concerned that it might break in the future
c
You could use an Int as a trigger, increment it to invalidate
☝️ 1
s
There’s also
neverEqualPolicy
as the
SnapshotMutationPolicy
to apply to a mutableState which will consider itself not equal to the previous value if you set the same value to it again. Not sure if that’s what you want here, but what you say kinda makes me think about it.
a
+1 to an incrementing counter, or a UUID that represents some sort of “session” that you can use as a key. That will keep you in the realm of normal Compose behavior. I’ve tried to get clever with custom
equals
, and almost immediately started getting confused by behavior.
y
What I want is akin to
SnapshopMutationPolicy
yes. I didn't want to keep an
Int
around because If I'm keeping a mutable object anyways then I might aswell just:
Copy code
data class State<S>(var state: S?)
remember { State<Blah>(null) }.apply { if(shouldUpdate || state == null) state = ... }
a
Having a
shouldUpdate
in there feels suspicious, what is driving the value of
shouldUpdate
?
y
It's derived from an implicit dependency. It's also derived from an
isEmpty
call on a piece of outside state. Both of these I can't really see being able to easily transform into the form that
remember
wants.