I need to refresh the data in a component page in an interval. Also I need to refresh the data after doing some action. I am using Obeservables in the service so that I can subscribe to when the response is ready. I am pushing the subscriptions to a object so that I can clear that on ngDestroy
, I think, I have the following methods to achieve the same.
Method 1 : setInterval
I have set an interval on ngOnInit
, which will call the refreshData in equal interval. The interval object will be cleared using clearInterval
in ngOnDestroy
method.
export class MyComponent implements OnInit, OnDestroy {
private subscription: Subscription = new Subscription();
data: any;
interval: any;
ngOnInit() {
this.refreshData();
this.interval = setInterval(() => {
this.refreshData();
}, 5000);
}
ngOnDestroy() {
this.subscription.unsubscribe();
clearInterval(this.interval);
}
refreshData(){
this.subscription.add(
this.myService.getData()
.subscribe(data => {
this.data = data;
})
);
}
doAction(){
this.subscription.add(
this.myService.doAction()
.subscribe(result => {
if(result === true){
this.refreshData();
}
})
);
}
}
Q1 : On each refresh call a subscription will be added to the subscription
object, will that increase the memory usage and may crash the browser if user keeps the page opened for a while?
Method2 : Observable.timer
This method is using a timer which will created after the data is refreshed.
export class MyComponent implements OnInit, OnDestroy {
private subscription: Subscription = new Subscription();
data: any;
ngOnInit() {
this.refreshData();
}
ngOnDestroy() {
this.subscription.unsubscribe();
}
refreshData(){
this.subscription.add(
this.myService.getData()
.subscribe(data => {
this.data = data;
this.subscribeToData();
})
);
}
subscribeToData(){
this.subscription.add(
Observable.timer(10000).subscribe(() => this.refreshData())
);
}
doAction(){
this.subscription.add(
this.myService.doAction()
.subscribe(result => {
if(result === true){
this.refreshData();
}
})
);
}
}
Q2 : I have the same question(Q1) here. This way, will add the timers also to the subscription object, so infact the subscriptions in the subscription object is doubled.
Q3 : To refresh data after the action method - doAction(), the refreshData is called. So will that create another chain of timer?
Q4 : Which is the better way without memory leaks or if there exists any other way?
You should be able to do this without problems:
ngOnInit() {
this.refreshData();
this.interval = setInterval(() => {
this.refreshData();
}, 5000);
}
refreshData(){
this.myService.getData()
.subscribe(data => {
this.data = data;
})
);
}
As per this post Angular will take care of cleaning up after itself.
However, if you're going to have a live data stream in your app I'd suggest changing your component so that rather than subscribing to each response of your service's http request, you instead subscribe once to a new observable data$
property of your service in your component's ngOnInit()
. Then, on interval (as you're doing) call updateData()
on your service (or setup the interval inside your service) but don't subscribe. When your service successfully fetches the data, it pushes the next value to its observable data$
property, giving you a stream of data from your service that you can react to anywhere in your app.
ngOnInit() {
this.myService.data$.subscribe(data => { // subscribe once to the data stream
this.data = data;
})
this.refreshData();
this.interval = setInterval(() => {
this.refreshData();
}, 5000);
}
refreshData(){
this.myService.updateData(); // simply signal for the service to update its data stream
}
With myService.data$
being an observable BehaviourSubject updated in your service, something like this:
public data$: BehaviorSubject<any> = new BehaviorSubject({});
updateData() {
let data = this.http.get('http://www.data.com').map((data)=>{
return data.json();
}).do((data)=>{
this.data$.next(data);
})
}
That way you can avoid multiple subscriptions and make the data stream available to any component that needs it.