You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Introduce Task.await and Task.asDeferred with CancellationTokenSource (Kotlin#2786)
* Support bi-directional cancellation for Task.asDeferred and Task.await via passed in CancellationTokenSource
FixesKotlin#2527
Co-authored-by: Alex Vanyo <[email protected]>
Co-authored-by: dkhalanskyjb <[email protected]>
Copy file name to clipboardExpand all lines: integration/kotlinx-coroutines-play-services/README.md
+10
Original file line number
Diff line number
Diff line change
@@ -6,6 +6,7 @@ Extension functions:
6
6
7
7
| **Name** | **Description**
8
8
| -------- | ---------------
9
+
| [Task.asDeferred][asDeferred] | Converts a Task into a Deferred
9
10
| [Task.await][await] | Awaits for completion of the Task (cancellable)
10
11
| [Deferred.asTask][asTask] | Converts a deferred value to a Task
11
12
@@ -25,5 +26,14 @@ val snapshot = try {
25
26
// Do stuff
26
27
```
27
28
29
+
If the `Task` supports cancellation via passing a `CancellationToken`, pass the corresponding `CancellationTokenSource` to `asDeferred` or `await` to support bi-directional cancellation:
30
+
31
+
```kotlin
32
+
val cancellationTokenSource =CancellationTokenSource()
33
+
val currentLocationTask = fusedLocationProviderClient.getCurrentLocation(PRIORITY_HIGH_ACCURACY, cancellationTokenSource.token)
34
+
val currentLocation = currentLocationTask.await(cancellationTokenSource) // cancelling `await` also cancels `currentLocationTask`, and vice versa
public final class kotlinx/coroutines/tasks/TasksKt {
2
2
public static final fun asDeferred (Lcom/google/android/gms/tasks/Task;)Lkotlinx/coroutines/Deferred;
3
+
public static final fun asDeferred (Lcom/google/android/gms/tasks/Task;Lcom/google/android/gms/tasks/CancellationTokenSource;)Lkotlinx/coroutines/Deferred;
3
4
public static final fun asTask (Lkotlinx/coroutines/Deferred;)Lcom/google/android/gms/tasks/Task;
5
+
public static final fun await (Lcom/google/android/gms/tasks/Task;Lcom/google/android/gms/tasks/CancellationTokenSource;Lkotlin/coroutines/Continuation;)Ljava/lang/Object;
4
6
public static final fun await (Lcom/google/android/gms/tasks/Task;Lkotlin/coroutines/Continuation;)Ljava/lang/Object;
0 commit comments