Skip to content

Commit

Permalink
LazyColumn에서 사용 시 메모리 누수가 발생하여 수정요청 드립니다. (#24)
Browse files Browse the repository at this point in the history
* LazyColumn 안에서 스크롤 해서 NaverMap이 안 보이는 시점에 메모리 해제가 되지 않아 메모리릭이 발생하는 현상, AnimatedNavHost 안에서 NaverMap을 사용할 때 onDestroy()가 호출되지 않는 현상을 해결하기 위해 코드를 수정합니다.

* Update naver-map-compose/src/main/java/com/naver/maps/map/compose/NaverMap.kt

Co-authored-by: Sungyong An <soupyong@gmail.com>

* googlemaps/android-maps-compose#138 과 비슷하게 코드를 수정합니다.

Co-authored-by: Sungyong An <soupyong@gmail.com>
  • Loading branch information
yU5295 and fornewid committed Jun 9, 2022
1 parent 4020713 commit 05d64a1
Showing 1 changed file with 25 additions and 1 deletion.
Expand Up @@ -27,7 +27,9 @@ import androidx.compose.runtime.Composition
import androidx.compose.runtime.CompositionContext
import androidx.compose.runtime.DisposableEffect
import androidx.compose.runtime.LaunchedEffect
import androidx.compose.runtime.MutableState
import androidx.compose.runtime.getValue
import androidx.compose.runtime.mutableStateOf
import androidx.compose.runtime.remember
import androidx.compose.runtime.rememberCompositionContext
import androidx.compose.runtime.rememberUpdatedState
Expand Down Expand Up @@ -164,10 +166,12 @@ private suspend inline fun MapView.awaitMap(): NaverMap {
private fun MapLifecycle(mapView: MapView) {
val context = LocalContext.current
val lifecycle = LocalLifecycleOwner.current.lifecycle
val previousState = remember { mutableStateOf(Lifecycle.Event.ON_CREATE) }
val savedInstanceState = rememberSavedInstanceState()
DisposableEffect(context, lifecycle, mapView, savedInstanceState) {
val mapLifecycleObserver = mapView.lifecycleObserver(
savedInstanceState.takeUnless { it.isEmpty }
savedInstanceState.takeUnless { it.isEmpty },
previousState,
)
val callbacks = mapView.componentCallbacks()

Expand All @@ -178,6 +182,24 @@ private fun MapLifecycle(mapView: MapView) {
mapView.onSaveInstanceState(savedInstanceState)
lifecycle.removeObserver(mapLifecycleObserver)
context.unregisterComponentCallbacks(callbacks)

// dispose 시점에 Lifecycle.Event가 끝까지 진행되지 않아 발생되는
// MapView Memory Leak 수정합니다.
when (previousState.value) {
Lifecycle.Event.ON_CREATE, Lifecycle.Event.ON_STOP -> {
mapView.onDestroy()
}
Lifecycle.Event.ON_START, Lifecycle.Event.ON_PAUSE -> {
mapView.onStop()
mapView.onDestroy()
}
Lifecycle.Event.ON_RESUME -> {
mapView.onPause()
mapView.onStop()
mapView.onDestroy()
}
else -> {}
}
}
}
}
Expand All @@ -189,6 +211,7 @@ private fun rememberSavedInstanceState(): Bundle {

private fun MapView.lifecycleObserver(
savedInstanceState: Bundle?,
previousState: MutableState<Lifecycle.Event>,
): LifecycleEventObserver {
return LifecycleEventObserver { _, event ->
when (event) {
Expand All @@ -200,6 +223,7 @@ private fun MapView.lifecycleObserver(
Lifecycle.Event.ON_DESTROY -> this.onDestroy()
else -> throw IllegalStateException()
}
previousState.value = event
}
}

Expand Down

0 comments on commit 05d64a1

Please sign in to comment.