Hello guys. I have one question to ask. I’m kinda new to Ionic so I wonder whether I could subscribe observable from service or not. If so, how can I unsubscribe it when the user signs out from my application?
This is my user service provider.
user-service.ts
getCurrentUsername() {
return this.afDB.object(`users/${this.currentUserId}/username`).valueChanges();
}
sendFriendRequest(recipient: string) {
let senderInfo = {
sender: this.currentUserId,
username: // I would like to include username as an object.
timestamp: Date.now(),
message: 'wants to be friend with you.'
}
return new Promise((resolve, reject) => {
this.afDB.list(`friend-requests/${recipient}`).push(senderInfo).then(() => {
resolve({'status': true, 'message': 'Friend request has sent.'});
}, error => reject({'status': false, 'message': error}));
});
}
A bad idea? I don’t think it’s a bad idea. A more complicated idea, yes. You could subscribe in your provider, and publish data to your pages with Ionic Events (in the event that data changes).
But yes, you can subscribe in your provider too, then just return the user or user name to the page you need it in. I personally tend not to, but only because it adds a layer of complicated to the process that I simply haven’t needed for the most part.
I just took another look at your question and realized that subscribing FROM the provider was you main concern. Sorry!
As mentioned above, yeah, you totally can. You would just have to add a function to your provider to unsubscribe from observedUser$ and call it on ionViewWillLeave()
Oh, I know what breaks my code.
I’m actually subscribing multiple things so that’s why I tried to use add() method. Anyways thank you, everything works fine now
ionViewWillLeave(){
this.subscriptionArray.filter(sub => sub !== undefined).map(subs => subs.unsubscribe())
}
I filter the subscription array for subscriptions that were not initialized based on code. But mapping the array of subscriptions to unsubscribe is probably what you’re looking for. At least, it’s a method that will work.
Using this.subscriptionArray.add(this.userSubscription) might work too, I just have never tried that syntax is all
(though I’m not sure .add is meant for that purpose)
Yeap I’ve done it like that
I’m sorry but one more thing @jaydz .
I’m currently unsubscribing from ngOnDestroy()
It seems like much more safer than using ionic lifecycle hook to me. What do you think about that?
Should I stick with the ionic page leave method?
ngOnDestroy() {
if (this.subscription !== undefined) {
this.subscription.unsubscribe();
}
}