From 88563d8b391ee3f44efa66901c47f6ac3d0c56aa Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Fred=20Porci=C3=BAncula?= Date: Thu, 15 Aug 2024 14:04:01 +0200 Subject: [PATCH] Add proper cancellation support on CurrentLocationScreen --- .../location/currentLocation/CurrentLocationScreen.kt | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/samples/location/src/main/java/com/example/platform/location/currentLocation/CurrentLocationScreen.kt b/samples/location/src/main/java/com/example/platform/location/currentLocation/CurrentLocationScreen.kt index dbe6bf0a..842f5a79 100644 --- a/samples/location/src/main/java/com/example/platform/location/currentLocation/CurrentLocationScreen.kt +++ b/samples/location/src/main/java/com/example/platform/location/currentLocation/CurrentLocationScreen.kt @@ -115,6 +115,7 @@ fun CurrentLocationContent(usePreciseLocation: Boolean) { onClick = { //To get more accurate or fresher device location use this method scope.launch(Dispatchers.IO) { + val cancellationTokenSource = CancellationTokenSource() val priority = if (usePreciseLocation) { Priority.PRIORITY_HIGH_ACCURACY } else { @@ -122,8 +123,8 @@ fun CurrentLocationContent(usePreciseLocation: Boolean) { } val result = locationClient.getCurrentLocation( priority, - CancellationTokenSource().token, - ).await() + cancellationTokenSource.token, + ).await(cancellationTokenSource) result?.let { fetchedLocation -> locationInfo = "Current location is \n" + "lat : ${fetchedLocation.latitude}\n" +