简体   繁体   English

退订对RXJS不起作用,正确的使用方法是什么?

[英]Unsubscribe does not work on RXJS, what is the correct method of use?

I made a code to monitor a map using nativescript-geolocation and nativescript-google-maps-sdk , there is an Angular service that encapsulates all access logic to nativescript-geolocation that receives a subscribe provided by a comment from Angular.我使用nativescript-geolocationnativescript-google-maps-sdk编写了一个代码来监视地图,有一个 Angular 服务封装了对nativescript-geolocation 的所有访问逻辑,该服务接收由 Angular 的评论提供的订阅。

When executing the listener represented by the component.mapViewRead() function, which runs when the map is ready, it subscribes to the subscribe service by calling a method servico.gpsInfo$.subscribe((gpsInfo) => this.gpsInfo = gpsInfo) , when the user manipulates the map by changing the camera position to a new coordinate, a property representing subscripition is used by another listener responsible for manipulating the camera to disable subscribe by calling the this.subscription.unsubscribe() .当执行由component.mapViewRead()函数表示的侦听器时,该函数在地图准备好时运行,它通过调用方法servico.gpsInfo$.subscribe((gpsInfo) => this.gpsInfo = gpsInfo)订阅订阅服务,当用户通过将相机位置更改为新坐标来操作地图时,表示订阅的属性由另一个负责操作相机的侦听器使用,以通过调用this.subscription.unsubscribe()来禁用订阅。

Although the subscription is theoretically disabled and can be entered through the this.subscription.isClosed property, it does not stop processing Observable calls.尽管订阅在理论上被禁用并且可以通过this.subscription.isClosed属性进入,但它不会停止处理 Observable 调用。

Am I doing something wrong?难道我做错了什么? or is it a RXJS bug?还是 RXJS 的错误?

Below is the code for both service and component.下面是服务和组件的代码。

At this link you can find the full code of the service, and below the code relevant to the problem. 在此链接中,您可以找到该服务的完整代码,以及与问题相关的代码下方。

startHeadingUpdates() {
        if (this.watchId) {
            return;
        }

        this.watchId = geoLocation.watchLocation(
            (loc) => {
                if (loc) {
this._gpsInfo.next(<GPSInfo>loc);
                }
            },
            (e) => {
                console.error("Error: " + e.message);
            },
            { desiredAccuracy: 3, updateDistance: 10, minimumUpdateTime: 1000 * .3 });
    }
    public get gpsInfo(): Observable<GPSInfo> {
        if (!this._gpsInfo$)
            this._gpsInfo$ = this._gpsInfo.asObservable()
        return this._gpsInfo$;
    }

Full component code can be found at this link , below only the code relevant to the problem:可以在此链接中找到完整的组件代码,下面仅列出与问题相关的代码:

// componente
    private subscribeGPSInfo() {
        if(this._gpsInfoSubscription && !this._gpsInfoSubscription.isClosed)
            this.unSubscribegpsInfo();

       this._gpsInfoSubscription = this._compass.gpsInfo.subscribe((gpsInfo) => {
            this.gpsInfo = gpsInfo;
        }, (error) => {         console.error("MussalaMapsComponent.ngAfterViewInit() _compass.gpsInfo.subscribe ", error);
        });
    }

    private unsubscribeGPSInfo() {
        if (this._gpsInfoSubscription && this._gpsInfoSubscription.closed) {
        this._gpsInfoSubscription.unsubscribe();
        }
    }

    goToMyLocation() {
        const cfg: GPSConfig = {};
        this._compass.getMyLocation(cfg)
            .then((gpsInfo: GPSInfo) => {
                this.gpsInfo = gpsInfo;
                this.mapView.latitude = this.gpsInfo.latitude;
                this.mapView.longitude = this.gpsInfo.longitude;
            });

        this.subscribeGPSInfo();
    }
    onMapReady(event) {
        const template = this.createInfoWindowTemplate();
        this.mapView.infoWindowTemplates = template;

        let marker: Marker = this._compass.createIslamicMarker(
            1,
            MakerType.MUSSALA,
            "Mussala Fortaleza",
            "Fortaleza, Ce, Brasil",
            "Rua São Paulo, 1831 - Jacarecanga, Fortaleza - CE, 60310-226",
            //-3.7214696,-38.5430259
            <GPSInfo>{ latitude: -3.7214696, longitude: -38.5430259 }
        );
        this.mapView.addMarker(marker);

        marker = this._compass.createIslamicMarker(
            2,
            MakerType.SPONSOR,
            "Curso Arduino Minas",
            "Aquiraz, Ce, Brasil",
            "R. José Alves Pereira, S/N, Aquiraz, CE, Brasil",
            {
                latitude: -3.9242100850690402,
                longitude: -38.45365650951862
            }
        );

        this.mapView.addMarker(marker);

        this.goToMyLocation();
        this.subscribeGPSInfo();

        this.isBusy = false;

    }

More Info:更多信息:

 npm list rxjs
IslamicApp@ C:\Users\Admin\workspace\islamic-works\Islamic-App
+-- @angular/cli@8.1.1
| +-- @angular-devkit/architect@0.801.1
| | `-- rxjs@6.4.0
| +-- @angular-devkit/core@8.1.1
| | `-- rxjs@6.4.0
| +-- @angular-devkit/schematics@8.1.1
| | `-- rxjs@6.4.0
| +-- @schematics/update@0.801.1
| | `-- rxjs@6.4.0
| `-- inquirer@6.4.1
|   `-- rxjs@6.5.2  deduped
+-- @nativescript/schematics@0.6.0
| +-- @angular-devkit/core@7.1.4
| | `-- rxjs@6.3.3
| `-- @angular-devkit/schematics@7.1.4
|   `-- rxjs@6.3.3  deduped
+-- @ngtools/webpack@8.0.6
| +-- @angular-devkit/core@8.0.6
| | `-- rxjs@6.4.0  deduped
| `-- rxjs@6.4.0
+-- nativescript-dev-webpack@1.0.1
| `-- @angular-devkit/core@8.0.0
|   `-- rxjs@6.4.0
`-- rxjs@6.5.2

I went to your Github link (now dead) to try and run the app to diagnose but it is not an app.我去你的 Github 链接(现在已经死了)尝试运行该应用程序进行诊断,但它不是一个应用程序。 It is a code snippet.这是一个代码片段。 Can you setup a small example app?你能设置一个小的示例应用程序吗? This will help you faster.这将帮助您更快。

In the meantime, it seems you are trying to unsubscribe from this._gpsInfoSubscription which is a result of this._compass.gpsInfo.subscribe(...) .与此同时,您似乎正在尝试取消订阅this._gpsInfoSubscription ,这是this._compass.gpsInfo.subscribe(...)结果

I would suggest you take a look at NetanelBasal/ngx-take-until-destroy .我建议你看看NetanelBasal/ngx-take-until-destroy Using rxjs pipe with ngx-take-until-destroy will help memory clean up more efficiently.将 rxjs pipengx-take-until-destroy将有助于更有效地清理内存。 I use it daily in my Angular applications.我每天都在我的 Angular 应用程序中使用它。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM