Unsubscribe does not work in RXJS, what is the correct method of use?

Asked

Viewed 65 times

-1

I made a code to monitor a map using the nativescript-Geolocation and the nativescript-google-maps-sdk, there is an Angular service that encapsulates all the logic of access to the nativescript-Geolocation receiving an subscribe provided by an Angular comment.

When executing the Listener represented by the function componente.mapViewRead(), which is executed when the map is ready, this subscriber in the subscribe service calling a method servico.gpsInfo$.subscribe((gpsInfo)=>this.gpsInfo = gpsInfo), when the user manipulates the map by changing the camera’s position to a new coordinate, a property that represents the subscripition is used by another Systener responsible for manipulating the camera to deactivate the subscribe by calling the function this.subscription.unsubscribe().

Although the subscription is deactivated theoretically and can be entered through the property this.subscription.isClosed, He won’t stop processing the Observable calls.

Am I doing something wrong? or is it a RXJS bug?

Below is the code of both service and component.

At this link can be found the full service code, and below the relevant code 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() {
        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;

    }
  • 1

    I did not find in your code where unsubscribeGPSInfo is called.

  • is after the call the goToMyLocation() function at the end of the displayed code.

  • @paulo-Antonelli the problem is in the version of RXJS, there is a version that has this bug, I switched to previous version and solved the problem. I ended up losing the solution link.

1 answer

1

In my experience with rxjs it always gives cm unsubscribe problems depending on the context. I solve these problems by creating a variable and typing it with the rxjs Subscription type and all observable that I will want to secure the unsubscribe I assign the subscribe call to that variable and when I need to make the unsubscribe just give I’m . unsubscribe() in the topada variable cm Subscription interrupting the observable on time. Ex:

export class Appcomponent { public registration: Subscription;

listenersDados(){ this.incricao = this.entradaDadosService.subscribe( (data) = > ...; ); } Interrupter Data() { If(this.inscription){ this.inscricao.unsubscribe() } } }

Browser other questions tagged

You are not signed in. Login or sign up in order to post.