For #4991:Ensured deleteSelectedBookmarks deletes all selected bookmarks (#5559)

Moved the deletion to a CoroutineScope(IO) to decouple from fragment lifecycle
Used local val tempStorage to keep reference to bookmarkStorage of outside class
Ran deletions with awaitAll to avoid concurrency issues on storage
This commit is contained in:
Mihai Adrian 2019-09-27 02:52:59 +03:00 committed by Sawyer Blatz
parent dffa61bf16
commit 62ae025d65

View File

@ -19,6 +19,8 @@ import androidx.lifecycle.lifecycleScope
import androidx.navigation.NavDirections
import androidx.navigation.fragment.findNavController
import kotlinx.android.synthetic.main.fragment_bookmark.view.*
import kotlinx.coroutines.async
import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.Dispatchers.IO
import kotlinx.coroutines.Dispatchers.Main
import kotlinx.coroutines.ExperimentalCoroutinesApi
@ -26,6 +28,7 @@ import kotlinx.coroutines.Job
import kotlinx.coroutines.ObsoleteCoroutinesApi
import kotlinx.coroutines.isActive
import kotlinx.coroutines.launch
import kotlinx.coroutines.awaitAll
import mozilla.appservices.places.BookmarkRoot
import mozilla.components.concept.storage.BookmarkNode
import mozilla.components.concept.storage.BookmarkNodeType
@ -257,8 +260,14 @@ class BookmarkFragment : LibraryPageFragment<BookmarkNode>(), BackHandler, Accou
}
private suspend fun deleteSelectedBookmarks(selected: Set<BookmarkNode>) {
selected.forEach {
context?.bookmarkStorage?.deleteNode(it.guid)
CoroutineScope(IO).launch {
val tempStorage = context?.bookmarkStorage
val deferreds = selected.map {
async {
tempStorage?.deleteNode(it.guid)
}
}
deferreds.awaitAll()
}
}