Skip to content

Commit

Permalink
fix: ANR caused by sync call to isEmpty()
Browse files Browse the repository at this point in the history
Closes OPENFOODFACTS-ANDROID-1R0
  • Loading branch information
VaiTon committed Apr 30, 2021
1 parent 60dd8d6 commit 32d69fc
Showing 1 changed file with 12 additions and 9 deletions.
Expand Up @@ -71,16 +71,19 @@ class TaxonomiesManager @Inject constructor(
val forceUpdate = mSettings.getBoolean(Utils.FORCE_REFRESH_TAXONOMIES, false)

// If database is empty or we have to force update, download it
if (dao.isEmpty() || forceUpdate) {
// Table is empty, no need check for update, just load taxonomy
return download(taxonomy, productRepository)
} else if (checkUpdate) {
// Get local last downloaded time
val localDownloadTime = mSettings.getLong(taxonomy.lastDownloadTimeStampPreferenceId, 0L)
// We need to check for update. Test if file on server is more recent than last download.
return checkAndDownloadIfNewer(taxonomy, localDownloadTime, productRepository)
return Single.fromCallable { dao.isEmpty() }.flatMap { empty ->
if (empty || forceUpdate) {
// Table is empty, no need check for update, just load taxonomy
download(taxonomy, productRepository)

} else if (checkUpdate) {
// Get local last downloaded time
val localDownloadTime = mSettings.getLong(taxonomy.lastDownloadTimeStampPreferenceId, 0L)

// We need to check for update. Test if file on server is more recent than last download.
checkAndDownloadIfNewer(taxonomy, localDownloadTime, productRepository)
} else Single.just(emptyList())
}
return Single.just(emptyList())
}

private fun <T> download(
Expand Down

0 comments on commit 32d69fc

Please sign in to comment.