0

The endpoint sends Server Sent Events, as strings and it is reactive(Same as Firebase). Each record corresponds to one line(or one string), then I map it to the actual data class.

I want it to create me only one list so I can feed it to the ui as state, but somehow it creates a loop and keepts creating multiple lists as shown in the logs and also recomposes the ui in the same loop.

This is the SSE call done with Ktor Client.

fun getStream(): Flow<Note?> = callbackFlow {
        try {
            client.sse(
                URL,
                request = {
                    headers.append(HttpHeaders.Accept, "text/event-stream")
                }, deserialize = { typeInfo, jsonString ->
                    val serializer = Json.serializersModule.serializer(typeInfo.kotlinType!!)
                    Json.decodeFromString(serializer, jsonString)!!
                }) {
                incoming.collect { event: TypedServerSentEvent<String> ->
                    val note: Note? = deserialize<Note>(event.data)
                    trySend(note)
                }
            }
        } catch (e: Exception) {
            e.printStackTrace()
        }
    }

This is how react to it in the viewmodel

class MainViewModel: ViewModel() {
    private val ktorNetwork = KtorNetwork()
    private val _uiState = MutableStateFlow<List<Note?>>(emptyList())
    val uiState = _uiState.asStateFlow()

init {
    getStream()
}

private fun getStream(){
    val list = mutableListOf<Note>()
    viewModelScope.launch {

        ktorNetwork.getStream()
            .distinctUntilChanged()
            .collect { note ->
            _uiState.update { currentList ->
                currentList + note
            }
        }
    }
}

UI:

@Composable
@Preview
fun App(
    viewModel: MainViewModel = MainViewModel()
) {

    val uiState = viewModel.uiState.collectAsState().value

    MaterialTheme {

        Column(
            modifier = Modifier
                .safeContentPadding()
                .fillMaxSize(),
            horizontalAlignment = Alignment.CenterHorizontally,
        ) {
            LazyColumn(
            ) {
                items(
                    items = uiState,
                    key = { note -> note?.id ?: 0 }
                ){
                    if (it != null) {
                        Text(
                            modifier = Modifier.fillMaxWidth(),
                            text = it.title
                        )
                    }
                }
            }
        }
    }
}

And it keeps rebuilding the list and recomposing the UI nonstop.

2025-06-15 13:32:59.543  9835-9835  System.out              com.mariomanhique.serversentevents   I  List of notes: [Note(id=1, title=Note1, content=My 1 ever note)]
2025-06-15 13:32:59.543  9835-9835  System.out              com.mariomanhique.serversentevents   I  Note: Note(id=2, title=Note2, content=My 2 ever note)
2025-06-15 13:32:59.543  9835-9835  System.out              com.mariomanhique.serversentevents   I  List of notes: [Note(id=1, title=Note1, content=My 1 ever note), Note(id=2, title=Note2, content=My 2 ever note)]
2025-06-15 13:32:59.607  9835-9835  System.out              com.mariomanhique.serversentevents   I  Note: Note(id=3, title=Note3, content=My 3 ever note)
2025-06-15 13:32:59.607  9835-9835  System.out              com.mariomanhique.serversentevents   I  List of notes: [Note(id=1, title=Note1, content=My 1 ever note), Note(id=2, title=Note2, content=My 2 ever note), Note(id=3, title=Note3, content=My 3 ever note)]
2025-06-15 13:32:59.607  9835-9835  System.out              com.mariomanhique.serversentevents   I  Note: Note(id=4, title=Note4, content=My 4 ever note)
2025-06-15 13:32:59.607  9835-9835  System.out              com.mariomanhique.serversentevents   I  List of notes: [Note(id=1, title=Note1, content=My 1 ever note), Note(id=2, title=Note2, content=My 2 ever note), Note(id=3, title=Note3, content=My 3 ever note), Note(id=4, title=Note4, content=My 4 ever note)]
2025-06-15 13:32:59.607  9835-9835  System.out              com.mariomanhique.serversentevents   I  Note: Note(id=5, title=Note5, content=My 5 ever note)
2025-06-15 13:32:59.607  9835-9835  System.out              com.mariomanhique.serversentevents   I  List of notes: [Note(id=1, title=Note1, content=My 1 ever note), Note(id=2, title=Note2, content=My 2 ever note), Note(id=3, title=Note3, content=My 3 ever note), Note(id=4, title=Note4, content=My 4 ever note), Note(id=5, title=Note5, content=My 5 ever note)]
2025-06-15 13:32:59.608  9835-9835  System.out              com.mariomanhique.serversentevents   I  Note: Note(id=6, title=Note6, content=My 6 ever note)
2025-06-15 13:32:59.608  9835-9835  System.out              com.mariomanhique.serversentevents   I  List of notes: [Note(id=1, title=Note1, content=My 1 ever note), Note(id=2, title=Note2, content=My 2 ever note), Note(id=3, title=Note3, content=My 3 ever note), Note(id=4, title=Note4, content=My 4 ever note), Note(id=5, title=Note5, content=My 5 ever note), Note(id=6, title=Note6, content=My 6 ever note)]
2025-06-15 13:32:59.608  9835-9835  System.out              com.mariomanhique.serversentevents   I  Note: Note(id=7, title=Note7, content=My 7 ever note)
2025-06-15 13:32:59.608  9835-9835  System.out              com.mariomanhique.serversentevents   I  List of notes: [Note(id=1, title=Note1, content=My 1 ever note), Note(id=2, title=Note2, content=My 2 ever note), Note(id=3, title=Note3, content=My 3 ever note), Note(id=4, title=Note4, content=My 4 ever note), Note(id=5, title=Note5, content=My 5 ever note), Note(id=6, title=Note6, content=My 6 ever note), Note(id=7, title=Note7, content=My 7 ever note)]
2025-06-15 13:32:59.609  9835-9854  EGL_emulation           com.mariomanhique.serversentevents   D  app_time_stats: avg=90.70ms min=3.32ms max=324.32ms count=12
2025-06-15 13:32:59.669  9835-9835  System.out              com.mariomanhique.serversentevents   I  Note: Note(id=8, title=Note8, content=My 8 ever note)
2025-06-15 13:32:59.669  9835-9835  System.out              com.mariomanhique.serversentevents   I  List of notes: [Note(id=1, title=Note1, content=My 1 ever note), Note(id=2, title=Note2, content=My 2 ever note), Note(id=3, title=Note3, content=My 3 ever note), Note(id=4, title=Note4, content=My 4 ever note), Note(id=5, title=Note5, content=My 5 ever note), Note(id=6, title=Note6, content=My 6 ever note), Note(id=7, title=Note7, content=My 7 ever note), Note(id=8, title=Note8, content=My 8 ever note)]
2025-06-15 13:32:59.670  9835-9835  System.out              com.mariomanhique.serversentevents   I  Note: Note(id=9, title=Note9, content=My 9 ever note)
2025-06-15 13:32:59.670  9835-9835  System.out              com.mariomanhique.serversentevents   I  List of notes: [Note(id=1, title=Note1, content=My 1 ever note), Note(id=2, title=Note2, content=My 2 ever note), Note(id=3, title=Note3, content=My 3 ever note), Note(id=4, title=Note4, content=My 4 ever note), Note(id=5, title=Note5, content=My 5 ever note), Note(id=6, title=Note6, content=My 6 ever note), Note(id=7, title=Note7, content=My 7 ever note), Note(id=8, title=Note8, content=My 8 ever note), Note(id=9, title=Note9, content=My 9 ever note)]
2025-06-15 13:32:59.670  9835-9835  System.out              com.mariomanhique.serversentevents   I  Note: Note(id=10, title=Note10, content=My 10 ever note)
2025-06-15 13:32:59.670  9835-9835  System.out              com.mariomanhique.serversentevents   I  List of notes: [Note(id=1, title=Note1, content=My 1 ever note), Note(id=2, title=Note2, content=My 2 ever note), Note(id=3, title=Note3, content=My 3 ever note), Note(id=4, title=Note4, content=My 4 ever note), Note(id=5, title=Note5, content=My 5 ever note), Note(id=6, title=Note6, content=My 6 ever note), Note(id=7, title=Note7, content=My 7 ever note), Note(id=8, title=Note8, content=My 8 ever note), Note(id=9, title=Note9, content=My 9 ever note), Note(id=10, title=Note10, content=My 10 ever note)]
2025-06-15 13:32:59.672  9835-9835  System.out              com.mariomanhique.serversentevents   I  Note: Note(id=11, title=Note11, content=My 11 ever note)
2025-06-15 13:32:59.672  9835-9835  System.out              com.mariomanhique.serversentevents   I  List of notes: [Note(id=1, title=Note1, content=My 1 ever note), Note(id=2, title=Note2, content=My 2 ever note), Note(id=3, title=Note3, content=My 3 ever note), Note(id=4, title=Note4, content=My 4 ever note), Note(id=5, title=Note5, content=My 5 ever note), Note(id=6, title=Note6, content=My 6 ever note), Note(id=7, title=Note7, content=My 7 ever note), Note(id=8, title=Note8, content=My 8 ever note), Note(id=9, title=Note9, content=My 9 ever note), Note(id=10, title=Note10, content=My 10 ever note), Note(id=11, title=Note11, content=My 11 ever note)]
2025-06-15 13:32:59.672  9835-9835  System.out              com.mariomanhique.serversentevents   I  Note: Note(id=12, title=Note12, content=My 12 ever note)
2025-06-15 13:32:59.672  9835-9835  System.out              com.mariomanhique.serversentevents   I  List of notes: [Note(id=1, title=Note1, content=My 1 ever note), Note(id=2, title=Note2, content=My 2 ever note), Note(id=3, title=Note3, content=My 3 ever note), Note(id=4, title=Note4, content=My 4 ever note), Note(id=5, title=Note5, content=My 5 ever note), Note(id=6, title=Note6, content=My 6 ever note), Note(id=7, title=Note7, content=My 7 ever note), Note(id=8, title=Note8, content=My 8 ever note), Note(id=9, title=Note9, content=My 9 ever note), Note(id=10, title=Note10, content=My 10 ever note), Note(id=11, title=Note11, content=My 11 ever note), Note(id=12, title=Note12, content=My 12 ever note)]
2025-06-15 13:32:59.673  9835-9835  System.out              com.mariomanhique.serversentevents   I  Note: Note(id=13, title=Note13, content=My 13 ever note)
2025-06-15 13:32:59.673  9835-9835  System.out              com.mariomanhique.serversentevents   I  List of notes: [Note(id=1, title=Note1, content=My 1 ever note), Note(id=2, title=Note2, content=My 2 ever note), Note(id=3, title=Note3, content=My 3 ever note), Note(id=4, title=Note4, content=My 4 ever note), Note(id=5, title=Note5, content=My 5 ever note), Note(id=6, title=Note6, content=My 6 ever note), Note(id=7, title=Note7, content=My 7 ever note), Note(id=8, title=Note8, content=My 8 ever note), Note(id=9, title=Note9, content=My 9 ever note), Note(id=10, title=Note10, content=My 10 ever note), Note(id=11, title=Note11, content=My 11 ever note), Note(id=12, title=Note12, content=My 12 ever note), Note(id=13, title=Note13, content=My 13 ever note)]
2025-06-15 13:32:59.781  9835-9835  System.out              com.mariomanhique.serversentevents   I  Note: Note(id=1, title=Note1, content=My 1 ever note)
2025-06-15 13:32:59.782  9835-9835  System.out              com.mariomanhique.serversentevents   I  List of notes: [Note(id=1, title=Note1, content=My 1 ever note)]
2025-06-15 13:32:59.782  9835-9835  System.out              com.mariomanhique.serversentevents   I  Note: Note(id=2, title=Note2, content=My 2 ever note)
2025-06-15 13:32:59.782  9835-9835  System.out              com.mariomanhique.serversentevents   I  List of notes: [Note(id=1, title=Note1, content=My 1 ever note), Note(id=2, title=Note2, content=My 2 ever note)]
2025-06-15 13:32:59.783  9835-9835  System.out              com.mariomanhique.serversentevents   I  Note: Note(id=3, title=Note3, content=My 3 ever note)
2025-06-15 13:32:59.783  9835-9835  System.out              com.mariomanhique.serversentevents   I  List of notes: [Note(id=1, title=Note1, content=My 1 ever note), Note(id=2, title=Note2, content=My 2 ever note), Note(id=3, title=Note3, content=My 3 ever note)]
2025-06-15 13:32:59.783  9835-9835  System.out              com.mariomanhique.serversentevents   I  Note: Note(id=4, title=Note4, content=My 4 ever note)
2025-06-15 13:32:59.783  9835-9835  System.out              com.mariomanhique.serversentevents   I  List of notes: [Note(id=1, title=Note1, content=My 1 ever note), Note(id=2, title=Note2, content=My 2 ever note), Note(id=3, title=Note3, content=My 3 ever note), Note(id=4, title=Note4, content=My 4 ever note)]
2025-06-15 13:32:59.783  9835-9835  System.out              com.mariomanhique.serversentevents   I  Note: Note(id=5, title=Note5, content=My 5 ever note)
2025-06-15 13:32:59.783  9835-9835  System.out              com.mariomanhique.serversentevents   I  List of notes: [Note(id=1, title=Note1, content=My 1 ever note), Note(id=2, title=Note2, content=My 2 ever note), Note(id=3, title=Note3, content=My 3 ever note), Note(id=4, title=Note4, content=My 4 ever note), Note(id=5, title=Note5, content=My 5 ever note)]
2025-06-15 13:32:59.783  9835-9835  System.out              com.mariomanhique.serversentevents   I  Note: Note(id=6, title=Note6, content=My 6 ever note)
2025-06-15 13:32:59.783  9835-9835  System.out              com.mariomanhique.serversentevents   I  List of notes: [Note(id=1, title=Note1, content=My 1 ever note), Note(id=2, title=Note2, content=My 2 ever note), Note(id=3, title=Note3, content=My 3 ever note), Note(id=4, title=Note4, content=My 4 ever note), Note(id=5, title=Note5, content=My 5 ever note), Note(id=6, title=Note6, content=My 6 ever note)]
2025-06-15 13:32:59.783  9835-9835  System.out              com.mariomanhique.serversentevents   I  Note: Note(id=7, title=Note7, content=My 7 ever note)
2025-06-15 13:32:59.783  9835-9835  System.out              com.mariomanhique.serversentevents   I  List of notes: [Note(id=1, title=Note1, content=My 1 ever note), Note(id=2, title=Note2, content=My 2 ever note), Note(id=3, title=Note3, content=My 3 ever note), Note(id=4, title=Note4, content=My 4 ever note), Note(id=5, title=Note5, content=My 5 ever note), Note(id=6, title=Note6, content=My 6 ever note), Note(id=7, title=Note7, content=My 7 ever note)]
2025-06-15 13:32:59.783  9835-9835  System.out              com.mariomanhique.serversentevents   I  Note: Note(id=8, title=Note8, content=My 8 ever note)
2025-06-15 13:32:59.783  9835-9835  System.out              com.mariomanhique.serversentevents   I  List of notes: [Note(id=1, title=Note1, content=My 1 ever note), Note(id=2, title=Note2, content=My 2 ever note), Note(id=3, title=Note3, content=My 3 ever note), Note(id=4, title=Note4, content=My 4 ever note), Note(id=5, title=Note5, content=My 5 ever note), Note(id=6, title=Note6, content=My 6 ever note), Note(id=7, title=Note7, content=My 7 ever note), Note(id=8, title=Note8, content=My 8 ever note)]
2025-06-15 13:32:59.783  9835-9835  System.out              com.mariomanhique.serversentevents   I  Note: Note(id=9, title=Note9, content=My 9 ever note)
2025-06-15 13:32:59.783  9835-9835  System.out              com.mariomanhique.serversentevents   I  List of notes: [Note(id=1, title=Note1, content=My 1 ever note), Note(id=2, title=Note2, content=My 2 ever note), Note(id=3, title=Note3, content=My 3 ever note), Note(id=4, title=Note4, content=My 4 ever note), Note(id=5, title=Note5, content=My 5 ever note), Note(id=6, title=Note6, content=My 6 ever note), Note(id=7, title=Note7, content=My 7 ever note), Note(id=8, title=Note8, content=My 8 ever note), Note(id=9, title=Note9, content=My 9 ever note)]
2025-06-15 13:32:59.783  9835-9835  System.out              com.mariomanhique.serversentevents   I  Note: Note(id=10, title=Note10, content=My 10 ever note)
2025-06-15 13:32:59.783  9835-9835  System.out              com.mariomanhique.serversentevents   I  List of notes: [Note(id=1, title=Note1, content=My 1 ever note), Note(id=2, title=Note2, content=My 2 ever note), Note(id=3, title=Note3, content=My 3 ever note), Note(id=4, title=Note4, content=My 4 ever note), Note(id=5, title=Note5, content=My 5 ever note), Note(id=6, title=Note6, content=My 6 ever note), Note(id=7, title=Note7, content=My 7 ever note), Note(id=8, title=Note8, content=My 8 ever note), Note(id=9, title=Note9, content=My 9 ever note), Note(id=10, title=Note10, content=My 10 ever note)]
2025-06-15 13:32:59.784  9835-9835  System.out              com.mariomanhique.serversentevents   I  Note: Note(id=11, title=Note11, content=My 11 ever note)
2025-06-15 13:32:59.784  9835-9835  System.out              com.mariomanhique.serversentevents   I  List of notes: [Note(id=1, title=Note1, content=My 1 ever note), Note(id=2, title=Note2, content=My 2 ever note), Note(id=3, title=Note3, content=My 3 ever note), Note(id=4, title=Note4, content=My 4 ever note), Note(id=5, title=Note5, content=My 5 ever note), Note(id=6, title=Note6, content=My 6 ever note), Note(id=7, title=Note7, content=My 7 ever note), Note(id=8, title=Note8, content=My 8 ever note), Note(id=9, title=Note9, content=My 9 ever note), Note(id=10, title=Note10, content=My 10 ever note), Note(id=11, title=Note11, content=My 11 ever note)]
2025-06-15 13:32:59.784  9835-9835  System.out              com.mariomanhique.serversentevents   I  Note: Note(id=12, title=Note12, content=My 12 ever note)
2025-06-15 13:32:59.784  9835-9835  System.out              com.mariomanhique.serversentevents   I  List of notes: [Note(id=1, title=Note1, content=My 1 ever note), Note(id=2, title=Note2, content=My 2 ever note), Note(id=3, title=Note3, content=My 3 ever note), Note(id=4, title=Note4, content=My 4 ever note), Note(id=5, title=Note5, content=My 5 ever note), Note(id=6, title=Note6, content=My 6 ever note), Note(id=7, title=Note7, content=My 7 ever note), Note(id=8, title=Note8, content=My 8 ever note), Note(id=9, title=Note9, content=My 9 ever note), Note(id=10, title=Note10, content=My 10 ever note), Note(id=11, title=Note11, content=My 11 ever note), Note(id=12, title=Note12, content=My 12 ever note)]
2025-06-15 13:32:59.785  9835-9835  System.out              com.mariomanhique.serversentevents   I  Note: Note(id=13, title=Note13, content=My 13 ever note)
2025-06-15 13:32:59.785  9835-9835  System.out              com.mariomanhique.serversentevents   I  List of notes: [Note(id=1, title=Note1, content=My 1 ever note), Note(id=2, title=Note2, content=My 2 ever note), Note(id=3, title=Note3, content=My 3 ever note), Note(id=4, title=Note4, content=My 4 ever note), Note(id=5, title=Note5, content=My 5 ever note), Note(id=6, title=Note6, content=My 6 ever note), Note(id=7, title=Note7, content=My 7 ever note), Note(id=8, title=Note8, content=My 8 ever note), Note(id=9, title=Note9, content=My 9 ever note), Note(id=10, title=Note10, content=My 10 ever note), Note(id=11, title=Note11, content=My 11 ever note), Note(id=12, title=Note12, content=My 12 ever note), Note(id=13, title=Note13, content=My 13 ever note)]
5
  • It's hard to tell what's going on without knowing what the endpoint actually sends and when. Please edit the question and clarify. Please also describe what the expected behavior would look like. Commented Jun 15 at 17:15
  • The endpoint sends Server Sent Events, as strings and It is reactive(Same as Firebase). Each record corresponds to one line(or one string), then I map it to the actual data class. I want it to create me only one list so I can feed it to the ui as state, but somehow it creates a loop and keepts creating multiple lists as shown in the logs and also recomposing the ui. Commented Jun 15 at 20:31
  • So, what does the endpoint send? Is it one event? Is it two? What does your KtorNetwork client actually register that the endpoint sends? Is it the right number of events? Does the view model receive the right number of events? Or is this purely an issue in Compose? Also, multiple lists can only be involved when you create multiple view model instances, since the view model list can never shrink, it can only grow. How do you create the view model? Please edit the question to make sure all relevant information is present so we can reproduce the issue (minimal reproducible example). Commented Jun 15 at 20:47
  • @MarioManhique Hello. Can you please edit the post and add the code in your UI? Commented Jun 21 at 6:14
  • Hi @Mahozad, I have added the code you requested. Commented Jun 22 at 20:51

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.