Fix: Memory Leak while syncing repo

This commit is contained in:
LooKeR 2021-10-23 00:28:50 +05:30
parent 424823dab2
commit 0512bc884a
3 changed files with 11 additions and 7 deletions

View File

@ -30,7 +30,7 @@ class MainApplication : Application(), ImageLoaderFactory {
val databaseUpdated = Database.init(this) val databaseUpdated = Database.init(this)
Preferences.init(this) Preferences.init(this)
ProductPreferences.init(this) ProductPreferences.init(this)
RepositoryUpdater.init(this) RepositoryUpdater.init()
listenApplications() listenApplications()
listenPreferences() listenPreferences()

View File

@ -57,12 +57,10 @@ object RepositoryUpdater {
} }
} }
private lateinit var context: Context
private val updaterLock = Any() private val updaterLock = Any()
private val cleanupLock = Any() private val cleanupLock = Any()
fun init(context: Context) { fun init() {
this.context = context
var lastDisabled = setOf<Long>() var lastDisabled = setOf<Long>()
Observable.just(Unit) Observable.just(Unit)
@ -93,18 +91,20 @@ object RepositoryUpdater {
} }
fun update( fun update(
context: Context,
repository: Repository, unstable: Boolean, repository: Repository, unstable: Boolean,
callback: (Stage, Long, Long?) -> Unit callback: (Stage, Long, Long?) -> Unit
): Single<Boolean> { ): Single<Boolean> {
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( private fun update(
context: Context,
repository: Repository, indexTypes: List<IndexType>, unstable: Boolean, repository: Repository, indexTypes: List<IndexType>, unstable: Boolean,
callback: (Stage, Long, Long?) -> Unit callback: (Stage, Long, Long?) -> Unit
): Single<Boolean> { ): Single<Boolean> {
val indexType = indexTypes[0] val indexType = indexTypes[0]
return downloadIndex(repository, indexType, callback) return downloadIndex(context, repository, indexType, callback)
.flatMap { (result, file) -> .flatMap { (result, file) ->
when { when {
result.isNotChanged -> { result.isNotChanged -> {
@ -115,6 +115,7 @@ object RepositoryUpdater {
file.delete() file.delete()
if (result.code == 404 && indexTypes.isNotEmpty()) { if (result.code == 404 && indexTypes.isNotEmpty()) {
update( update(
context,
repository, repository,
indexTypes.subList(1, indexTypes.size), indexTypes.subList(1, indexTypes.size),
unstable, unstable,
@ -132,6 +133,7 @@ object RepositoryUpdater {
else -> { else -> {
RxUtils.managedSingle { RxUtils.managedSingle {
processFile( processFile(
context,
repository, indexType, unstable, repository, indexType, unstable,
file, result.lastModified, result.entityTag, callback file, result.lastModified, result.entityTag, callback
) )
@ -142,6 +144,7 @@ object RepositoryUpdater {
} }
private fun downloadIndex( private fun downloadIndex(
context: Context,
repository: Repository, indexType: IndexType, repository: Repository, indexType: IndexType,
callback: (Stage, Long, Long?) -> Unit callback: (Stage, Long, Long?) -> Unit
): Single<Pair<Downloader.Result, File>> { ): Single<Pair<Downloader.Result, File>> {
@ -179,6 +182,7 @@ object RepositoryUpdater {
} }
private fun processFile( private fun processFile(
context: Context,
repository: Repository, indexType: IndexType, unstable: Boolean, repository: Repository, indexType: IndexType, unstable: Boolean,
file: File, lastModified: String, entityTag: String, callback: (Stage, Long, Long?) -> Unit file: File, lastModified: String, entityTag: String, callback: (Stage, Long, Long?) -> Unit
): Boolean { ): Boolean {

View File

@ -348,7 +348,7 @@ class SyncService : ConnectionService<SyncService.Binder>() {
val unstable = Preferences[Preferences.Key.UpdateUnstable] val unstable = Preferences[Preferences.Key.UpdateUnstable]
lateinit var disposable: Disposable lateinit var disposable: Disposable
disposable = RepositoryUpdater disposable = RepositoryUpdater
.update(repository, unstable) { stage, progress, total -> .update(this, repository, unstable) { stage, progress, total ->
if (!disposable.isDisposed) { if (!disposable.isDisposed) {
stateSubject.onNext( stateSubject.onNext(
State.Syncing( State.Syncing(