离子与取消订阅可观察对象的AngularFire2最佳实践 [英] Ionic & AngularFire2 best practice for unsubscribing to observables

查看:98
本文介绍了离子与取消订阅可观察对象的AngularFire2最佳实践的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个多页面的Ionic-Angular电话应用程序,该应用程序使用AngularFire2&消防站.我真正喜欢Firestore的一件事是数据的离线持久性.我的问题围绕着取消订阅AngularFire2流可观察对象的数据持久性和最佳实践.

I have a multi-page Ionic-Angular phone app that uses AngularFire2 & Firestore. One of the things I really like about Firestore is offline persistence of data. My question revolves around this data persistence and best practices for unsubscribing to AngularFire2 stream observables.

我的主页是事件列表,选择事件后,应用程序将路由到具有该事件详细信息的单事件页面.目前,我根据单事件组件ngOnInit中Firestore中所选事件的文档ID,使用AngularFire2 API订阅了单事件.

My home page is a list of events and when an event is selected the app routes to a single-event page with this specific event's details. Currently I subscribe to the single-event using AngularFire2 API based on the selected event's doc ID in Firestore within the single-event component ngOnInit.

我对Angular中的可观察对象的理解是,建议从某个组件中导航出来时退订可观察对象.但是,如果我在单事件组件中执行此操作,并且用户以某种方式失去了互联网连接,那么当他们导航到主页然后又返回到单事件时,将没有数据,因为流可观察对象已取消订阅,并且应用程序无法联系Firestore.现在,如果我不退订,那么即使处于脱机状态,单事件页面仍会显示数据.这是我想要的行为,但是在离开单事件页面时我不取消订阅可观察对象是否是一个问题?

My understanding of observables in Angular is that it is recommended to unsubscribe to observables when navigating back out of a component. However, if I do this in the single-event component and the user somehow loses internet connectivity, when they navigate to home page and then back to a single-event, there is no data since the stream observables have been unsubscribed and the app cannot contact Firestore. Now if I do not unsubscribe, then the single-event page still displays data even if offline. This is the behavior that I want, but is it a problem somehow that I am not unsubscribing to observables when leaving single-event page?

即使在基于Ionic的电话应用中,取消订阅可观察对象是否也很重要?还是应该重新设计我的应用程序以实现所有最佳实践? 感谢您的任何投入!

Is it important to unsubscribe to observables even in an Ionic based phone app? Or should I be re-architecting my app to achieve all best practices? Thanks for any inputs!

推荐答案

即使在基于Ionic的电话应用中,取消订阅可观察对象是否也很重要?

Is it important to unsubscribe to observables even in an Ionic based phone app?

是的,否则,如果您要离开该页面并返回它,则会为该可观察对象创建另一个订阅,这将导致内存泄漏.

Yes, otherwise if you were to leave the page and come back it would create another subscription to the same observable which would cause memory leaks.

我知道有几种方法可以在您离开时停止订阅.
1.创建,分配和取消预订Subscription.

There are a couple ways that I know of to stop subscriptions when you nav away.
1. Create, assign and unsubscribe from a Subscription.

foo$: Observable<Foo>;
fooSub: Subscription;

constructor() {
  this.fooSub = this.foo$.subscribe(foo => { console.log(foo); });
}

ionViewDidLeave() {
  this.fooSub.unsubscribe();
}

2.创建一个布尔值以限制订阅运行的时间.

2. Create a boolean to limit when the subscription runs.

active: boolean = true;
foo$: Observable<Foo>;

constructor() {
  this.foo$.takeWhile(() => this.active).subscribe(foo => { console.log(foo); });
}

ionViewDidLeave() {
  this.active = false;
}

对于IonicPage组件使用ionViewDidLeave,对于自定义组件使用ngOnDestroy.

Use ionViewDidLeave for IonicPage components and ngOnDestroy for custom components.

如果您希望数据在设备离线时仍然存在,则应查看 ngrx/store .

If you want data to persist while the device is offline, you should look into ngrx/store.

这篇关于离子与取消订阅可观察对象的AngularFire2最佳实践的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆