LocationCallback() { override fun onLocationResult(result: LocationResult?) { result ?: return try { offer(result.lastLocation) } catch(e: Exception) {} } } requestLocationUpdates(/* ... */) .addOnFailureListener { e -> close(e) // in case of exception, close the Flow } // clean up when Flow collection ends awaitClose { removeLocationUpdates(callback) } } asLiveData vs lifecycleScope.launchWhenStarted
coroutines • Exceptions in Coroutines • The suspend modifier — Under the hood goo.gle/coroutines-101 • Coroutines 101 codelabs.developers.google.com • Use Kotlin Coroutines in your Android App • Learn advanced coroutines with Kotlin Flow and LiveData • Building a Kotlin extensions library