Reputation: 19
I am using ionic 3 for my app,i am using cordova-plugin-network-information for internet connection detection event in android device, i am able to subscribe it, but not able to unsubscribe it.
I have tried rxjs, Subscription to unsubscribe from the observables, but didnt get expected result.
following is my code :
import {Subscription } from 'rxjs/Subscription';
private _netConnection : Subscription = new Subscription();
_netConnection = this.network.onConnect().subscribe(()=>{
alert("connected");
})
_netConnection.unsubscribe();
But the enter code here
above _netConnection.unsubscribe() is not working, as i am still getting the alert when connection is available.
Upvotes: 0
Views: 412
Reputation: 21638
Use take until
import { Subject } from 'rxjs';
import { takeUntil } from 'rxjs/operators';
private finalize = new Subject<void>();
this.network.onConnect().pipe(takeUntil(finalise)).subscribe(()=>{
alert("connected");
finalise.next();
});
Upvotes: 1
Reputation: 51
I think u should write the _netConnection.unsubscribe()
in setTimeout
;
like this setTimeout(()=> _netConnection.unsubscribe())
;
Upvotes: 0