从 RxJava 链中设置变量
Setting Variable From Within RxJava Chain
我正在使用ReactiveLocation
library。基本上,我想在 4 秒内获得足够准确的位置。如果当时没有收到足够准确的位置,但其他位置已经收到,则 return 最准确的位置。
一旦接收到足够准确的位置,就会 returned 并完成观察。
我会post我正在尝试的代码。我可能,而且可能正在以错误的方式解决这个问题。
LocationRequest request = LocationRequest.create()
.setPriority(LocationRequest.PRIORITY_HIGH_ACCURACY)
.setFastestInterval(FASTEST_UPDATE_INTERVAL)
.setSmallestDisplacement(MIN_DISTANCE_UPDATE_INTERVAL);
if ( expirationSeconds != null )
request.setExpirationDuration(TimeUnit.SECONDS.toMillis(expirationSeconds));
ReactiveLocationProvider locationProvider = new ReactiveLocationProvider(context);
Observable<Location> observable = locationProvider.getUpdatedLocation(request)
.doOnNext(new Action1<Location>() {
@Override
public void call(Location location) {
if ( mostAccurateLocation == null )
mostAccurateLocation = location;
if ( location.getAccuracy() < mostAccurateLocation.getAccuracy() )
mostAccurateLocation = location;
}
})
.filter(new Func1<Location, Boolean>() {
@Override
public Boolean call(Location location) {
return location.getAccuracy() < sufficientAccuracy ;
}
});
if ( expirationSeconds != null )
observable = observable.timeout( expirationSeconds, TimeUnit.SECONDS, Observable.just(mostAccurateLocation), backgroundThread );
return observable.firstOrDefault(mostAccurateLocation)
.doOnNext(new Action1<Location>() {
@Override
public void call(Location location) {
lastLocation = location;
}
});
好的,我找到问题了。超时开始了,但显然,由超时启动的可观察对象正在使用 mostAccurateLocation
变量,因为它是在可观察对象创建时使用的。
为了解决这个问题,我使用了 Observable.defer
模式,因此它只会在订阅时创建 Observable<Location>
。
if ( expirationSeconds != null ) {
Observable<Location> mostAccurateLocationObservable = Observable.defer(new Func0<Observable<Location>>() {
@Override
public Observable<Location> call() {
return Observable.just(mostAccurateLocation);
}
});
observable = observable.timeout( expirationSeconds, TimeUnit.SECONDS, mostAccurateLocationObservable, backgroundThread );
}
我正在使用ReactiveLocation
library。基本上,我想在 4 秒内获得足够准确的位置。如果当时没有收到足够准确的位置,但其他位置已经收到,则 return 最准确的位置。
一旦接收到足够准确的位置,就会 returned 并完成观察。
我会post我正在尝试的代码。我可能,而且可能正在以错误的方式解决这个问题。
LocationRequest request = LocationRequest.create()
.setPriority(LocationRequest.PRIORITY_HIGH_ACCURACY)
.setFastestInterval(FASTEST_UPDATE_INTERVAL)
.setSmallestDisplacement(MIN_DISTANCE_UPDATE_INTERVAL);
if ( expirationSeconds != null )
request.setExpirationDuration(TimeUnit.SECONDS.toMillis(expirationSeconds));
ReactiveLocationProvider locationProvider = new ReactiveLocationProvider(context);
Observable<Location> observable = locationProvider.getUpdatedLocation(request)
.doOnNext(new Action1<Location>() {
@Override
public void call(Location location) {
if ( mostAccurateLocation == null )
mostAccurateLocation = location;
if ( location.getAccuracy() < mostAccurateLocation.getAccuracy() )
mostAccurateLocation = location;
}
})
.filter(new Func1<Location, Boolean>() {
@Override
public Boolean call(Location location) {
return location.getAccuracy() < sufficientAccuracy ;
}
});
if ( expirationSeconds != null )
observable = observable.timeout( expirationSeconds, TimeUnit.SECONDS, Observable.just(mostAccurateLocation), backgroundThread );
return observable.firstOrDefault(mostAccurateLocation)
.doOnNext(new Action1<Location>() {
@Override
public void call(Location location) {
lastLocation = location;
}
});
好的,我找到问题了。超时开始了,但显然,由超时启动的可观察对象正在使用 mostAccurateLocation
变量,因为它是在可观察对象创建时使用的。
为了解决这个问题,我使用了 Observable.defer
模式,因此它只会在订阅时创建 Observable<Location>
。
if ( expirationSeconds != null ) {
Observable<Location> mostAccurateLocationObservable = Observable.defer(new Func0<Observable<Location>>() {
@Override
public Observable<Location> call() {
return Observable.just(mostAccurateLocation);
}
});
observable = observable.timeout( expirationSeconds, TimeUnit.SECONDS, mostAccurateLocationObservable, backgroundThread );
}