https://kotlinlang.org logo
Title
a

Ahmad Hassan

03/22/2023, 2:32 PM
Hi, I’m using decompose in my Compose Multiplatform app, Android is working fine but iOS is not working as expected. I think, there’s an issue in the state on the iOS side, recomposition is not triggering properly. Did anyone face this issue? • Already using the experimental version
a

Arkadii Ivanov

03/22/2023, 2:59 PM
Could you please share some of the Composable's code, where re-composition is not triggered?
a

Ahmad Hassan

03/22/2023, 3:05 PM
internal fun NoteListScreen(
    viewModel: NoteListViewModelComponent
) {

    val state by viewModel.state.collectAsState()

    LaunchedEffect(key1 = true) {
        viewModel.loadNotes()
    }
    
    Scaffold(
        floatingActionButton = {
            FloatingActionButton(
                onClick = {
                    viewModel.onAddORItemClicked(null)
                },
                backgroundColor = Color.Black,
            ) {
                Icon(
                    imageVector = Icons.Default.Add,
                    contentDescription = "Add Note",
                    tint = Color.White
                )
            }
        },
    ) { padding ->
        Column(
            modifier = Modifier
                .fillMaxSize()
                .padding(padding),
        ) {
            Box(
                modifier = Modifier.fillMaxWidth(),
                contentAlignment = Alignment.Center
            ) {
                HideableSearchTextField(
                    text = state.searchText,
                    onTextChange = viewModel::onSearchTextChange,
                    isSearchActive = state.isSearchActive,
                    onSearchClick = viewModel::onToggleSearch,
                    onCloseClick = viewModel::onToggleSearch,
                    modifier = Modifier
                        .fillMaxWidth()
                        .height(90.dp)
                )
                this@Column.AnimatedVisibility(
                    visible = !state.isSearchActive,
                    enter = fadeIn(),
                    exit = fadeOut()
                ) {
                    Text(
                        text = "All Notes",
                        fontWeight = FontWeight.Bold,
                        fontSize = 30.sp
                    )
                }
            }
            LazyColumn(
                modifier = Modifier
                    .weight(1f)
            ) {
                items(
                    items = state.notes,
                    key = { it.id!! }
                ) { note ->
                    NoteItem(
                        note = note,
                        backgroundColor = Color(note.colorHex),
                        onNoteClick = {
                            viewModel.onAddORItemClicked(note.id)
                        },
                        onDeleteClick = {
                            viewModel.deleteNote(note.id!!)
                        },
                        modifier = Modifier
                            .fillMaxWidth()
                            .padding(16.dp)
                            .animateItemPlacement()
                    )

                }
            }
        }
    }

}
a

Arkadii Ivanov

03/22/2023, 3:29 PM
It appears that
NoteListViewModelComponent#state
is of type
Flow
. The code looks correct. But since Decompose components are just normal classes, this looks a bit unrelated to Decompose. Please make sure that the state is actually updated in the component. You can add some logs to verify that. Also a common mistake with Decompose + Compose for iOS - make sure you have called root's
LifecycleRegistery.resume
after you created the root component.
a

Ahmad Hassan

03/24/2023, 8:24 AM
Well, it's working fine on Android. The only issue in iOS. And I also used LifecycleRegistery.resume still, same issue.
This issue is also on the Desktop. I deleted the middle note, a third note should become the second. I don’t think, there’s any issue in the code because everything is fine on Android. And it’s a single codebase (Compose Multiplatform).
a

Arkadii Ivanov

03/24/2023, 10:50 AM
Even though your code is the same, there are 3 different implementations of compose under the hood . So sometimes things work differently
I recommend simplifying the code gradually until you end up with something very small. If the issue is still there, you can ask in Compose channels. If not, then you will know what's the cause.