Skip to content

Commit

Permalink
Clear also keys when cleaning up a Container
Browse files Browse the repository at this point in the history
  • Loading branch information
sebaslogen committed Aug 20, 2024
1 parent cc4f03e commit e8747e1
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -358,6 +358,7 @@ public class ScopedViewModelContainer : ViewModel(), LifecycleEventObserver {
val lastObject = objectsToClear.removeLast()
clearLastDisposedObject(lastObject, objectsToClear)
}
scopedObjectKeys.clear() // Clear all keys
scopedObjectsContainer.clear() // Clear just in case this VM is leaked
super.onCleared()
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package com.sebaslogen.resacaapp.sample.ui.main.data
import android.os.Bundle
import androidx.lifecycle.SavedStateHandle
import androidx.lifecycle.ViewModel
import com.sebaslogen.resacaapp.sample.ui.main.compose.objectToShortStringWithoutPackageName
import com.sebaslogen.resacaapp.sample.ui.main.data.FakeScopedViewModel.Companion.MY_ARGS_KEY
import com.sebaslogen.resacaapp.sample.viewModelsClearedGloballySharedCounter
import java.util.concurrent.atomic.AtomicInteger
Expand All @@ -26,6 +27,11 @@ class FakeScopedViewModel(private val stateSaver: SavedStateHandle) : ViewModel(
*/
private val viewModelsClearedCounter: AtomicInteger = viewModelsClearedGloballySharedCounter

/**
* Memory address of the ViewModel instance to debug name on the screen
*/
val memoryAddress: String = objectToShortStringWithoutPackageName(this).replaceBeforeLast("@", "")

override fun onCleared() {
viewModelsClearedCounter.incrementAndGet()
super.onCleared()
Expand Down

0 comments on commit e8747e1

Please sign in to comment.