![](/img/trans.png)
[英]rxjava + retrofit - How to wait and get the result of first observable in android?
[英]Get Result from Observable - Kotin Android
我有一个在 Observable 上返回的方法,如下所示:
fun currentLocation(): Observable<LatLngFix> {
if (GoogleApiAvailability.getInstance().isGooglePlayServicesAvailable(context) != ConnectionResult.SUCCESS ||
!context.isLocationPermissionGranted()) {
return getDefaultLocation()
}
// lastKnownLocation returns location or complete without result
// in the latter case, return default unknown location
return Observable
.concat(locationProvider.lastKnownLocation.map {
val latLng = LatLngFix(it.latitude, it.longitude)
return@map latLng
}, getDefaultLocation())
.onErrorResumeNext { getDefaultLocation() }
.first()
}
然后当我调用这个函数时,我想得到 latlng 变量。 所以我显然需要订阅Observable,但不知道为什么,以下给出了一个错误:
localisationService.currentLocation().subscribe(latLng -> {
})
说 latLng 没有定义。 检索值的正确方法是什么?
应该
localisationService.currentLocation().subscribe { latLng ->
}
(注意大括号内的 latLng 参数)
或者
localisationService.currentLocation().subscribe ({ latLng ->
},
{ error ->
})
如果您还想实现onError
。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.