From 0258fc5e29141a2ebdf529cffdb65d2297077ea1 Mon Sep 17 00:00:00 2001 From: tibbi Date: Sun, 17 Jun 2018 22:02:52 +0200 Subject: [PATCH] ignore ConcurrentModificationExceptions thrown at updating the directories --- .../gallery/activities/MainActivity.kt | 60 ++++++++++--------- 1 file changed, 32 insertions(+), 28 deletions(-) diff --git a/app/src/main/kotlin/com/simplemobiletools/gallery/activities/MainActivity.kt b/app/src/main/kotlin/com/simplemobiletools/gallery/activities/MainActivity.kt index 1fc066316..1068bc419 100644 --- a/app/src/main/kotlin/com/simplemobiletools/gallery/activities/MainActivity.kt +++ b/app/src/main/kotlin/com/simplemobiletools/gallery/activities/MainActivity.kt @@ -37,6 +37,7 @@ import com.simplemobiletools.gallery.models.Directory import com.simplemobiletools.gallery.models.Medium import kotlinx.android.synthetic.main.activity_main.* import java.io.* +import java.util.* class MainActivity : SimpleActivity(), DirectoryAdapter.DirOperationsListener { private val PICK_MEDIA = 2 @@ -609,41 +610,44 @@ class MainActivity : SimpleActivity(), DirectoryAdapter.DirOperationsListener { val directoryDao = galleryDB.DirectoryDao() val getProperDateTaken = config.directorySorting and SORT_BY_DATE_TAKEN != 0 - for (directory in dirs) { - val curMedia = mediaFetcher.getFilesFrom(directory.path, getImagesOnly, getVideosOnly, getProperDateTaken) - val newDir = if (curMedia.isEmpty()) { - directory - } else { - createDirectoryFromMedia(directory.path, curMedia, albumCovers, hiddenString, includedFolders, isSortingAscending) - } + try { + for (directory in dirs) { + val curMedia = mediaFetcher.getFilesFrom(directory.path, getImagesOnly, getVideosOnly, getProperDateTaken) + val newDir = if (curMedia.isEmpty()) { + directory + } else { + createDirectoryFromMedia(directory.path, curMedia, albumCovers, hiddenString, includedFolders, isSortingAscending) + } - // we are looping through the already displayed folders looking for changes, do not do anything if nothing changed - if (directory == newDir) { - continue - } + // we are looping through the already displayed folders looking for changes, do not do anything if nothing changed + if (directory == newDir) { + continue + } - directory.apply { - tmb = newDir.tmb - name = newDir.name - mediaCnt = newDir.mediaCnt - modified = newDir.modified - taken = newDir.taken - this@apply.size = newDir.size - types = newDir.types - } + directory.apply { + tmb = newDir.tmb + name = newDir.name + mediaCnt = newDir.mediaCnt + modified = newDir.modified + taken = newDir.taken + this@apply.size = newDir.size + types = newDir.types + } - showSortedDirs(dirs) + showSortedDirs(dirs) - // update directories and media files in the local db, delete invalid items - updateDBDirectory(directory) - mediumDao.insertAll(curMedia) - getCachedMedia(directory.path, getVideosOnly, getImagesOnly) { - it.forEach { - if (!curMedia.contains(it)) { - mediumDao.deleteMediumPath(it.path) + // update directories and media files in the local db, delete invalid items + updateDBDirectory(directory) + mediumDao.insertAll(curMedia) + getCachedMedia(directory.path, getVideosOnly, getImagesOnly) { + it.forEach { + if (!curMedia.contains(it)) { + mediumDao.deleteMediumPath(it.path) + } } } } + } catch (ignored: ConcurrentModificationException) { } val foldersToScan = mediaFetcher.getFoldersToScan()