Skip to content

Collecting unstable values as state causes the presenter to loop re-creation #1891

Answered by stagg
octoeddy asked this question in Q&A
Discussion options

You must be logged in to vote

Just looking at the repro, think the bigger issue is that each invoke of the use case will be returning a new Flow. Which would also be unstable and cause more compositions.

  val result by useCase().collectAsRetainedState(null)

It’s a subtle issue when using collectAs*, where if the Flow isn't a single instance it needs to remember too.

One option is to use a produce*State to "capture" the use case invoke and remember it while its collected.

  val result by
    produceRetainedState<Result<*>?>(initialValue = null, key) {
      useCase().collect { value = it }
    }

Replies: 1 comment 1 reply

Comment options

You must be logged in to vote
1 reply
@octoeddy
Comment options

Answer selected by octoeddy
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
Labels
None yet
2 participants