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)
Preferences.init(this)
ProductPreferences.init(this)
RepositoryUpdater.init(this)
RepositoryUpdater.init()
listenApplications()
listenPreferences()

View File

@ -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<Long>()
Observable.just(Unit)
@ -93,18 +91,20 @@ object RepositoryUpdater {
}
fun update(
context: Context,
repository: Repository, unstable: Boolean,
callback: (Stage, Long, Long?) -> Unit
): 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(
context: Context,
repository: Repository, indexTypes: List<IndexType>, unstable: Boolean,
callback: (Stage, Long, Long?) -> Unit
): Single<Boolean> {
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<Pair<Downloader.Result, File>> {
@ -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 {

View File

@ -348,7 +348,7 @@ class SyncService : ConnectionService<SyncService.Binder>() {
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(