Skip to content
Permalink
Browse files

Fix potential memory leak

Since we're now using a potentially longer
lived scope for data operations (#397), we need
to make sure we don't leak any ViewModel members.

Fixed by wrapping the ObservableLoadingCounter
to a WeakReference.
  • Loading branch information...
chrisbanes committed Aug 14, 2019
1 parent 902d61b commit e74b5d4797262de5eafc0e4812698f13fc33a5f9
Showing with 12 additions and 6 deletions.
  1. +12 −6 interactors/src/main/java/app/tivi/interactors/Interactor.kt
@@ -22,12 +22,14 @@ import io.reactivex.BackpressureStrategy
import io.reactivex.subjects.BehaviorSubject
import kotlinx.coroutines.CoroutineDispatcher
import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.Job
import kotlinx.coroutines.flow.Flow
import kotlinx.coroutines.flow.distinctUntilChanged
import kotlinx.coroutines.flow.flatMapLatest
import kotlinx.coroutines.launch
import kotlinx.coroutines.reactive.asFlow
import kotlinx.coroutines.withContext
import java.lang.ref.WeakReference

interface Interactor<in P> {
val dispatcher: CoroutineDispatcher
@@ -74,20 +76,24 @@ fun <P> CoroutineScope.launchInteractor(
interactor: Interactor<P>,
param: P,
loadingCounter: ObservableLoadingCounter? = null
) = launch(context = interactor.dispatcher) {
loadingCounter?.addLoader()
interactor(param)
loadingCounter?.removeLoader()
): Job {
val loadingCounterWeakRef = loadingCounter?.let { WeakReference(it) }
return launch(context = interactor.dispatcher) {
loadingCounterWeakRef?.get()?.addLoader()
interactor(param)
loadingCounterWeakRef?.get()?.removeLoader()
}
}

suspend fun <P> Interactor<P>.execute(
param: P,
loadingCounter: ObservableLoadingCounter? = null
) {
val loadingCounterWeakRef = loadingCounter?.let { WeakReference(it) }
withContext(context = dispatcher) {
loadingCounter?.addLoader()
loadingCounterWeakRef?.get()?.addLoader()
invoke(param)
loadingCounter?.removeLoader()
loadingCounterWeakRef?.get()?.removeLoader()
}
}

0 comments on commit e74b5d4

Please sign in to comment.
You can’t perform that action at this time.