I have this repository : @Singleton class ImageRepository @Inject constructor(…
- User
I have this repository :
@Singleton class ImageRepository @Inject constructor( @ApplicationContext private val context: Context, ) {
// Initialize Realm instance private val realm: Realm = App.realm // Flow of processed images from Realm val processedImages: Flow<List<ImageLabel>> = realm .query<RealMImageLabel>() .asFlow() .map { realmResults -> realmResults.list.map { it.toImageLabel() } } // Function to add a processed image to Realm suspend fun addProcessedImage(imageLabel: ImageLabel) = withContext(Dispatchers.IO) { realm.write { copyToRealm(imageLabel.toRealmImageLabel(), updatePolicy = UpdatePolicy.ALL) } } // Flow of all images (processed and in progress) val allImages: Flow<List<Media>> = flow { // This assumes queryImages provides a Flow emit(queryImages(context)) } // Clean up resources when the repository is no longer needed fun close() { realm.close() }}
this viewmodel:
class ImagesViewmodel @Inject constructor( @ApplicationContext val context: Context, private val repository: ImageRepository, ) : ScreenModel { // Flow for processed images val processedImages: StateFlow<List> = repository.processedImages .stateIn(screenModelScope, SharingStarted.Lazily, emptyList())
// Flow for in-progress images // Flow for all images val allImages: StateFlow<List<Media>> = combine( repository.allImages.distinctUntilChanged(), processedImages ) { allImages, processedIds -> val processedimageIds = processedIds.map { it.imageId } allImages.sortedBy { it.time }.map { media -> media.copy( processed = media.id in processedimageIds) } } .distinctUntilChanged() .stateIn(screenModelScope, SharingStarted.Lazily, emptyList())and this is my screen: @Composable override fun Content() { val navigator = LocalNavigator.currentOrThrow val viewModel = navigator.getNavigatorScreenModel() val allImages by viewModel.allImages.collectAsState(emptyList()) val processedImages by viewModel.processedImages.collectAsState(emptyList()) val activityEvents = LocalActivityEventsProvider.current activityEvents?.events?.collectAsEffect(key = activityEvents.events) { when (it) { ActivityEvents.ActivityTrigger.StartSync -> { viewModel.startImageProcessingService() } } } LaunchedEffect( allImages ) { //log it Log.wtf("HomeScreen", "Processed Images: $allImages") }
Column { //lazy grid that contains items for processed and unprocessed images LazyVerticalGrid( columns = GridCells.Fixed(3), modifier = Modifier .fillMaxSize() .padding(horizontal = 20.dp) .background(Color.Blue), horizontalArrangement = Arrangement.spacedBy(4.dp) ) { item(span = { GridItemSpan(maxLineSpan) }) { Text(text = "All Images") } items(processedImages){ } items(allImages) { image -> ImageGridItem( imageUri = image.uri, processed = image.processed ) } } } } - Assistant
The performance issue you're experiencing stems from the way Compose handles recompositions in lists like
LazyVerticalGrid. When any item in yourallImageslist changes (even if it's just theprocessedflag), Compose sees the entire list as changed because the default method in data classes considers all properties. This causes the entire list to be re-emitted and the UI to recompose all items, which is inefficient.