[Android] Use withContext to wait for an async operation

Geoffrey Métais git at videolan.org
Tue Feb 27 15:27:10 CET 2018


vlc-android | branch: master | Geoffrey Métais <geoffrey.metais at gmail.com> | Tue Feb 27 14:12:43 2018 +0100| [2787624027b0c5a482bfa3711ed34fb002c2d833] | committer: Geoffrey Métais

Use withContext to wait for an async operation

> https://code.videolan.org/videolan/vlc-android/commit/2787624027b0c5a482bfa3711ed34fb002c2d833
---

 vlc-android/src/org/videolan/vlc/gui/DiffUtilAdapter.kt | 5 +++--
 1 file changed, 3 insertions(+), 2 deletions(-)

diff --git a/vlc-android/src/org/videolan/vlc/gui/DiffUtilAdapter.kt b/vlc-android/src/org/videolan/vlc/gui/DiffUtilAdapter.kt
index 5e0011676..4cba6d0a3 100644
--- a/vlc-android/src/org/videolan/vlc/gui/DiffUtilAdapter.kt
+++ b/vlc-android/src/org/videolan/vlc/gui/DiffUtilAdapter.kt
@@ -9,6 +9,7 @@ import kotlinx.coroutines.experimental.channels.Channel
 import kotlinx.coroutines.experimental.channels.actor
 import kotlinx.coroutines.experimental.launch
 import kotlinx.coroutines.experimental.newSingleThreadContext
+import kotlinx.coroutines.experimental.withContext
 import java.util.*
 
 abstract class DiffUtilAdapter<D, VH : RecyclerView.ViewHolder> : RecyclerView.Adapter<VH>() {
@@ -32,11 +33,11 @@ abstract class DiffUtilAdapter<D, VH : RecyclerView.ViewHolder> : RecyclerView.A
     private suspend fun internalUpdate(list: List<D>) {
         val finalList = prepareList(list)
         val result = DiffUtil.calculateDiff(diffCallback.apply { update(dataset, finalList) }, detectMoves())
-        launch(UI) {
+        withContext(UI) {
             dataset = finalList
             result.dispatchUpdatesTo(this at DiffUtilAdapter)
             onUpdateFinished()
-        }.join()
+        }
     }
 
     protected open fun prepareList(list: List<D>) : List<D> = ArrayList(list)



More information about the Android mailing list