How can I unsubscribe to subscribed object?

Hello :slightly_smiling_face:

What I want to do is that when User Login firstly, I want to redirect to profile page
and on profile page, user would inscribe more detailed information(this info is saved to '/profile/user uid’
then, when this user logged in secondly and so on, It directly redirect to HomePage.

I think…many of you guys can have a same process like this. How did you solve this?

When user Logged in(I use google plus login),

this.googleplus.login({
      'webClientId':'xxxxxxxxxx'
    }).then((res)=>{
      
      firebase.auth().signInWithCredential(firebase.auth.GoogleAuthProvider.credential
       (res.idToken)).then(
        suc=>{
          this.afAuth.authState.subscribe(auth=>{
            
            this.items=this.afDatabase.list('profile/'+auth.uid, { preserveSnapshot: true })
            this.items.subscribe(snapshots=>{
              
              if(snapshots.length==0){
                 //if profile is not made , I put null data so that I can tell
                this.profile.first=false;
                this.profile.id="null";
                this.profile.created_date="null";
                this.profile.foto="null";
                this.profile.notiId="";
                this.profile.uid="";
                this.afDatabase.object('profile/'+auth.uid+'/').set(this.profile)

                     **//I think I should unsubscribe here...because on profile page, profile/uid 's
                   data is changed, and with this subscribe , data is get messed..**

                .then(() => {  this.navCtrl.setRoot(ProfilePage,{uid:auth.uid,tokenId:this.tokenId});   } ).catch((error)=> alert("err : "+error))
              }else{
                  this.navCtrl.setRoot(HomePage)
}

You tagged this Ionic 1. Are you in Ionic 1?

no…It is 2 …

Then change the tag please. To answer your question:

import { Subscription } from 'rxjs/Subscription';
thingToUnsubscribeFromLater: Subscription = new Subscription();
thingToUnsubscribeFromLater = someObservable.subscribe(x => console.log(x));
thingToUnsubscribeFromLater.unsubscribe();
2 Likes

superb :slight_smile: Thank you!