0

I have a list of strings where I store network images that I later iterate through within a forEach loop, and within it, I have the GlideImage composable that allows me to determine if the image was loaded correctly. However, when I add a variable to track how many images were loaded correctly, the counter never stops.

fun ImageLoader() {
    val urls = listOf(
        "https://www.example.com/image1.jpg",
        "https://www.example.com/image2.jpg",
        "https://www.example.com/image3.jpg",
        "https://www.example.com/image1.jpg",
    )
    val successfulImageCount = remember { mutableStateOf(0) }

    Column(modifier = Modifier.fillMaxSize()) {
        urls.forEach { url ->
            GlideImage(
                imageModel = { url },
                success = { status: GlideImageState.Success, image: Painter ->
                    successfulImageCount.value++
                    Image(painter = image, contentDescription = "")
                },
                modifier = Modifier.size(80.dp),
            )
        }

        Text(text = "Images uploaded correctly: ${successfulImageCount.value}")
    }
}
1
  • Updating the counter causes ImageLoader to recompose, which cause GlideImage to recompose, which update the counter again, looping infinitely. Commented May 2, 2024 at 4:41

1 Answer 1

0

To fix the issue where updating the counter causes the ImageLoader to recompose, which in turn recomposes GlideImage, updating the counter again and causing an infinite loop, you can use a side effect to update the counter only once per image load. Here's how you can do it:

@Composable
fun ImageLoader() {
    val urls = listOf(
        "https://www.example.com/image1.jpg",
        "https://www.example.com/image2.jpg",
        "https://www.example.com/image3.jpg",
        "https://www.example.com/image1.jpg",
    )
    val successfulImageCount = remember { mutableStateOf(0) }

    Column(modifier = Modifier.fillMaxSize()) {
        urls.forEach { url ->
            GlideImage(
                imageModel = { url },
                success = { status: GlideImageState.Success, image: Painter ->
                    // Use a side-effect to increment the counter only once per image load
                    DisposableEffect(key1 = url) {
                        successfulImageCount.value++
                        onDispose { }
                    }
                    Image(painter = image, contentDescription = "")
                },
                modifier = Modifier.size(80.dp),
            )
        }

        Text(text = "Images uploaded correctly: ${successfulImageCount.value}")
    }
}
Sign up to request clarification or add additional context in comments.

Comments

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.