Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Compose for Desktop doesn't emit lifecycle events, only states #4835

Closed
SebastianAigner opened this issue May 19, 2024 · 4 comments
Closed
Assignees
Labels
desktop question Not a bug, but question or comment

Comments

@SebastianAigner
Copy link
Member

SebastianAigner commented May 19, 2024

1.6.10-rc03.

iOS:

Screenshot 2024-05-19 at 11 28 03

Desktop:

Screenshot 2024-05-19 at 11 28 27

Observed via:

@Composable
fun LifecycleLogger() {
    val lc = LocalLifecycleOwner.current
    LaunchedEffect(lc) {
        launch {
            lc.lifecycle.currentStateFlow.collect {
                println("New State: $it")
            }
        }
        launch {
            lc.lifecycle.eventFlow.collect {
                println("New Event: $it")
            }
        }
    }
}
@SebastianAigner SebastianAigner added bug Something isn't working submitted labels May 19, 2024
@zsmb13
Copy link

zsmb13 commented May 19, 2024

Looking at the impl of eventFlow:

public val Lifecycle.eventFlow: Flow<Lifecycle.Event>
    get() = callbackFlow {
        val observer = LifecycleEventObserver { _, event ->
            trySend(event)
        }.also { addObserver(it) }

        awaitClose { removeObserver(observer) }
    }.flowOn(Dispatchers.Main.immediate)

Apparently it uses Dispatchers.Main, which is not available if you don't add coroutines-swing to the project explicitly. Adding this dependency fixes the issue:

implementation("org.jetbrains.kotlinx:kotlinx-coroutines-swing:1.8.1")

@SebastianAigner
Copy link
Member Author

Very strange that I'm seeing no indication (warning, error, log output) indicating this.

@eymar eymar added desktop and removed submitted labels May 21, 2024
@MatkovIvan MatkovIvan added question Not a bug, but question or comment and removed bug Something isn't working labels May 21, 2024
@MatkovIvan MatkovIvan self-assigned this May 21, 2024
@MatkovIvan
Copy link
Member

It's expected because kotlinx-coroutines-swing is optional. I'll check why a warning message is missed + make sure that it's desired in docs

@MatkovIvan
Copy link
Member

Rechecked what's going on here.

Compose for Desktop doesn't emit lifecycle events

It does. Even without this extra dependency. You can receive it via:

lifecycle.addObserver(object : LifecycleEventObserver {
    override fun onStateChanged(source: LifecycleOwner, event: Lifecycle.Event) {
        println("New Event: $event")
    }

})

Very strange that I'm seeing no indication (warning, error, log output) indicating this.

It looks like behavior of flowOn function + missed Dispatchers.Main. Example:

flowOf(1, 2, 3)
    .flowOn(Dispatchers.Main.immediate)
    .collect {
        println("collect: $it")
    }

Will print nothing and hang on collect function without any exceptions. I guess it is out of the scope of Compose Multiplatfrom repo, but maybe worth creating YT issue for coroutines library.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
desktop question Not a bug, but question or comment
Projects
None yet
Development

No branches or pull requests

4 participants