【发布时间】:2017-01-25 23:07:33
【问题描述】:
我正在向我的应用程序添加地理定位,我需要在用户实际请求位置之前的某个时间启动 observable Geolocation.watchPostion()。
我有创建实例变量的想法
private locationObserver = (stub observer);
private locationSubscription;
然后做
this.locationSubscription = Geolocation.watchPosition().subscribe(
next => this.locationObserver.next(next),
err => this.locationObserver.error(error),
comp => this.locationObserver.complete(complete)
)
然后在稍后调用以下命令。
onRequestLocation(){
this.locationObserver = Observer.create(
location => {
this.doMyStuff(location);
this.locationSubscription.unsubscribe();
}
)
}
但是,这并不是正确的做法。这很糟糕吗?
我的目标是启动Geolocation.watchPosition(),然后响应用户事件,开始做某事。我还希望在用户事件之后 5 秒发生超时事件。
这是另一种感觉仍然不对的可能性:
我制作实例变量
private userEventHasOccured
private locationSubscription
然后做
this.locationSubscription = Geolocation.watchPosition().subscribe(
next => {
if (userEventHasOccured) {
this.doMyStuff(next);
this.unsubscribeLocation();
}
}
)
public unsubscribeLocation() {
this.locationSubscription.unsubscribe();
}
和
onUserEvent(){
this.userEventHasOccured = true;
}
另外,有没有一种不那么烦人的订阅方式?
【问题讨论】:
标签: angular ionic-framework ionic2 rxjs