我正在尝试使用 Rx-Java 在 Android 上创建一个用于位置跟踪的类。我仍然无法弄清楚如何正确处理我的 Observable 的生命周期。我想要的是一个 Observable,它在第一次订阅发生时开始跟踪位置,并在最后一次订阅被丢弃时停止位置跟踪。到目前为止,我取得的成就是:
public class LocationObservable
implements GooglePlayServicesClient.ConnectionCallbacks,
GooglePlayServicesClient.OnConnectionFailedListener,
LocationListener {
private LocationClient locationClient;
private final PublishSubject<Location> latestLocation =
PublishSubject.create();
public final Observable<Location> locationObservable =
Observable.defer(() -> {
if(!locationClient.isConnected() && !locationClient.isConnecting()) {
locationClient.connect();
}
return latestLocation.asObservable().scan((prev, curr) -> {
if (Math.abs(prev.getLatitude() - curr.getLatitude()) > 0.000001 ||
Math.abs(prev.getLongitude() - curr.getLongitude()) > 0.000001)
return curr;
else
return prev;
}).distinctUntilChanged();});
public LocationObservable(Context context) {
locationClient = new LocationClient(context, this, this);
}
@Override
public void onConnected(Bundle bundle) {
latestLocation.onNext(locationClient.getLastLocation());
}
@Override
public void onDisconnected() {
latestLocation.onCompleted();
}
@Override
public void onConnectionFailed(ConnectionResult connectionResult) {
latestLocation.onError(new Exception(connectionResult.toString()));
}
@Override
public void onLocationChanged(Location location) {
latestLocation.onNext(location);
}
}
如您所见,Observable#defer
当第一个客户端订阅时,我使用它来初始化位置回调。我不知道这是否是一个好方法,但这是我目前想出的最好的方法。我仍然缺少的是当我班的最后一个客户取消订阅我的 observable 时如何停止位置更新。或者也许它在 Rx 中是非惯用的,因为它并不明显?
我相信,这个用例应该是相当标准的,因此应该有一个标准/惯用的解决方案。很高兴知道它。