cashapp / redwood

Multiplatform reactive UI for Android, iOS, and web using Kotlin and Jetpack Compose
https://cashapp.github.io/redwood/0.x/docs/
Apache License 2.0
1.61k stars 70 forks source link

Disposal callbacks aren't called when disposing of a TreehouseUi #2248

Open dellisd opened 4 weeks ago

dellisd commented 4 weeks ago

The following does not work when a TreehouseUi screen is closed/disposed of on both Android and iOS:

DisposableEffect(Unit) {
  println("Hello")

  onDispose {
    // Never called!
    println("World!")
  }
}

A workaround is to use a LaunchedEffect:

LaunchedEffect(Unit) {
  try {
    println("Hello")
    awaitCancellation()
  } finally {
    // This prints fine
    println("World")
  }
}
JakeWharton commented 4 weeks ago

If you are tearing down the whole Zipline instance, what's probably happening is that this is running after the binding through which logs are sent is removed.

dellisd commented 4 weeks ago

When you say Zipline instance, do you mean for a single screen?

My understanding was that a Zipline instance would survive for the lifetime of the treehouse app, but I'm seeing this issue when tearing down a single screen, e.g. navigating back from a treehouse screen.

JakeWharton commented 4 weeks ago

I don't know how it's wired internally, but I would expect each screen to be a Zipline instance, yes. I can look next week.

swankjesse commented 2 weeks ago

The Zipline instance spans multiple screens, so I think this is likely a lifecycle step I’ve missed in the Treehouse part of Redwood.