Fix crash when manually refresh repo db

This commit is contained in:
topjohnwu 2020-01-14 22:03:29 +08:00
parent 47c3045980
commit de853a2651

View File

@ -39,7 +39,7 @@ class RepoUpdater(
it.error()?.also { throw it } it.error()?.also { throw it }
it.response()?.run { it.response()?.run {
if (code() == HttpURLConnection.HTTP_NOT_MODIFIED) if (code() == HttpURLConnection.HTTP_NOT_MODIFIED)
return@run Flowable.error<Unit>(CachedException) return@run Flowable.error<Unit>(CachedException())
if (page == 1) if (page == 1)
repoDB.etagKey = headers()[Const.Key.ETAG_KEY].orEmpty().trimEtag() repoDB.etagKey = headers()[Const.Key.ETAG_KEY].orEmpty().trimEtag()
@ -64,21 +64,23 @@ class RepoUpdater(
@Suppress("RedundantLambdaArrow") @Suppress("RedundantLambdaArrow")
operator fun invoke(forced: Boolean = false) : Completable { operator fun invoke(forced: Boolean = false) : Completable {
val cached = Collections.synchronizedSet(HashSet(repoDB.repoIDList)) return Flowable.fromCallable { Collections.synchronizedSet(HashSet(repoDB.repoIDList)) }
return loadPage(cached, etag = repoDB.etagKey).doOnComplete { .flatMap { cached ->
repoDB.removeRepos(cached) loadPage(cached, etag = repoDB.etagKey).doOnComplete {
}.onErrorResumeNext { it: Throwable -> repoDB.removeRepos(cached)
if (it is CachedException) { }.onErrorResumeNext { it: Throwable ->
if (forced) if (it is CachedException) {
return@onErrorResumeNext forcedReload(cached) if (forced)
} else { return@onErrorResumeNext forcedReload(cached)
Timber.e(it) } else {
} Timber.e(it)
Flowable.empty() }
}.ignoreElements() Flowable.empty()
}
}.ignoreElements()
} }
object CachedException : Exception() class CachedException : Exception()
} }
private val dateFormat: SimpleDateFormat = private val dateFormat: SimpleDateFormat =