0

I want to remove item from the LazyColumn. I have a bunch of item in the list i.e. 200+ item. When I removed item it little bit slow. I don't understand what is the problem in my code. I am adding basic sample what I did try on my code.

RemoveItem

@Composable
fun RemoveItem(viewModel: RemovedItemViewModel) {
    var newList = viewModel.itemList
    LaunchedEffect(key1 = viewModel.itemList) {
        newList = viewModel.itemList
    }

    LazyColumn {
        items(newList) {
            Text(text = "$it")
        }
    }
}

RemovedItemViewModel

class RemovedItemViewModel : BaseViewModel() {
    val itemList = mutableStateListOf<MovieItem>()

    init {
        repeat(10) {
            itemList.add(MovieItem("$it cast", "Movie $it"))
        }
    }

    fun removeItem() {
        val random = (0..10).random()
        itemList.removeAt(random)
    }
}

MovieItem

data class MovieItem(val number: String, val name: String)

Is there any better way to remove item?

Kotlin Learner
  • 3,995
  • 6
  • 47
  • 127
  • Your `newList` variable and LaunchedEffect are completely pointless. It might be triggering redundant recompositions, but I'm not sure. – Tenfour04 May 31 '23 at 16:54
  • so should I remove it ? – Kotlin Learner May 31 '23 at 16:54
  • 1
    Yes, it should be removed, although I don't know if it's causing your performance issue. It's likely bad for performance, but it might not be the primary problem. – Tenfour04 May 31 '23 at 16:55
  • Ok I'll try to remove – Kotlin Learner May 31 '23 at 16:56
  • 1
    For your learning, we can step through your logic to see why it's pointless. The LaunchedEffect is using that list as a key. This launches a coroutine that will change the variable value. But unless the coroutine is running in an `immediate` dispatcher, it will be finishing after this composition is already done. It is updating an old, obsolete version of the `newList` variable that can never be used. All of this is entirely pointless anyway because that variable ultimately only points at the same list you're using as a key, so you're not actually changing the variable value. – Tenfour04 May 31 '23 at 17:07
  • Hey @Tenfour04 I added my code in [here](https://stackoverflow.com/q/76523494/8266651). Can you please guide me on this? – Kotlin Learner Jun 21 '23 at 13:14

1 Answers1

1

You can have performance problems for several reasons.

The first reason is that you are using items without a key. In order for list item operations to be more optimized you need to use a unique key. If it is not unique, you will get a runtime exception, so be careful.

The second issue is the unnecessary LaunchedEffect as already noticed on the comments

Also, in order to determine if you really have problems with performance, I would recommend that you first to check your app with isDebuggable = false in your build.gradle file as well as the using release build with enabled R8. Here is a very big difference between debug and release modes when we're using compose, read more here.

If despite everything that I have listed above you still encounter performance problems, then I would recommend that you install a latest preview version of Android Studio Hedgehog. In it you can check why unnecessary recompositions happen by debugging the composition state

tasjapr
  • 632
  • 4
  • 13
  • Hey @tasjapr I added my code in [here](https://stackoverflow.com/q/76523494/8266651). can you please take a look in here. Thanks – Kotlin Learner Jun 21 '23 at 13:13