From 0512bc884aa099948ccc834378364efc03fd37dd Mon Sep 17 00:00:00 2001 From: LooKeR Date: Sat, 23 Oct 2021 00:28:50 +0530 Subject: [PATCH] Fix: Memory Leak while syncing repo --- .../kotlin/com/looker/droidify/MainApplication.kt | 2 +- .../com/looker/droidify/index/RepositoryUpdater.kt | 14 +++++++++----- .../com/looker/droidify/service/SyncService.kt | 2 +- 3 files changed, 11 insertions(+), 7 deletions(-) diff --git a/src/main/kotlin/com/looker/droidify/MainApplication.kt b/src/main/kotlin/com/looker/droidify/MainApplication.kt index 9f882828..f332473e 100644 --- a/src/main/kotlin/com/looker/droidify/MainApplication.kt +++ b/src/main/kotlin/com/looker/droidify/MainApplication.kt @@ -30,7 +30,7 @@ class MainApplication : Application(), ImageLoaderFactory { val databaseUpdated = Database.init(this) Preferences.init(this) ProductPreferences.init(this) - RepositoryUpdater.init(this) + RepositoryUpdater.init() listenApplications() listenPreferences() diff --git a/src/main/kotlin/com/looker/droidify/index/RepositoryUpdater.kt b/src/main/kotlin/com/looker/droidify/index/RepositoryUpdater.kt index 4091ccd3..a1cafb3e 100644 --- a/src/main/kotlin/com/looker/droidify/index/RepositoryUpdater.kt +++ b/src/main/kotlin/com/looker/droidify/index/RepositoryUpdater.kt @@ -57,12 +57,10 @@ object RepositoryUpdater { } } - private lateinit var context: Context private val updaterLock = Any() private val cleanupLock = Any() - fun init(context: Context) { - this.context = context + fun init() { var lastDisabled = setOf() Observable.just(Unit) @@ -93,18 +91,20 @@ object RepositoryUpdater { } fun update( + context: Context, repository: Repository, unstable: Boolean, callback: (Stage, Long, Long?) -> Unit ): Single { - return update(repository, listOf(IndexType.INDEX_V1, IndexType.INDEX), unstable, callback) + return update(context, repository, listOf(IndexType.INDEX_V1, IndexType.INDEX), unstable, callback) } private fun update( + context: Context, repository: Repository, indexTypes: List, unstable: Boolean, callback: (Stage, Long, Long?) -> Unit ): Single { val indexType = indexTypes[0] - return downloadIndex(repository, indexType, callback) + return downloadIndex(context, repository, indexType, callback) .flatMap { (result, file) -> when { result.isNotChanged -> { @@ -115,6 +115,7 @@ object RepositoryUpdater { file.delete() if (result.code == 404 && indexTypes.isNotEmpty()) { update( + context, repository, indexTypes.subList(1, indexTypes.size), unstable, @@ -132,6 +133,7 @@ object RepositoryUpdater { else -> { RxUtils.managedSingle { processFile( + context, repository, indexType, unstable, file, result.lastModified, result.entityTag, callback ) @@ -142,6 +144,7 @@ object RepositoryUpdater { } private fun downloadIndex( + context: Context, repository: Repository, indexType: IndexType, callback: (Stage, Long, Long?) -> Unit ): Single> { @@ -179,6 +182,7 @@ object RepositoryUpdater { } private fun processFile( + context: Context, repository: Repository, indexType: IndexType, unstable: Boolean, file: File, lastModified: String, entityTag: String, callback: (Stage, Long, Long?) -> Unit ): Boolean { diff --git a/src/main/kotlin/com/looker/droidify/service/SyncService.kt b/src/main/kotlin/com/looker/droidify/service/SyncService.kt index d3be6497..4d997851 100644 --- a/src/main/kotlin/com/looker/droidify/service/SyncService.kt +++ b/src/main/kotlin/com/looker/droidify/service/SyncService.kt @@ -348,7 +348,7 @@ class SyncService : ConnectionService() { val unstable = Preferences[Preferences.Key.UpdateUnstable] lateinit var disposable: Disposable disposable = RepositoryUpdater - .update(repository, unstable) { stage, progress, total -> + .update(this, repository, unstable) { stage, progress, total -> if (!disposable.isDisposed) { stateSubject.onNext( State.Syncing(