aut*_*tic 1 firebase angular google-cloud-firestore angular-akita
我有一个CollectionService()关于akita-ng-fire模块中可用方法的查询。我创建了一个扩展 CollectionService() 的服务,并使用 syncCollection 来维护 Firestore 文档和我的 Web 客户端之间的同步。这是服务定义的样子:
@Injectable({providedIn: 'root'})
@CollectionConfig({path: 'someDoc/:customId/anotherDoc'})
export class MyService extends CollectionService<WorkspaceState> {
constructor(store: WorkspaceStore) {
super(store);
this.store.setHasCache(true, {restartTTL: true});
}
// service methods ......
}
Run Code Online (Sandbox Code Playgroud)
我onInit在组件的指令中使用它来初始化同步。
ngOnInit() {
this.sub = this.myService.syncCollection().pipe(
concatMap(_ => this.query.myDoc$.pipe(
tap(d => this.myService.markActive(d.customId)),
tap(d => this.customId = d.customId),
)),
tap(d => this.router.navigate(['somePlace', d. customId])),
).subscribe();
}
Run Code Online (Sandbox Code Playgroud)
但是,我看到此同步每分钟读取约 5 次。有没有办法减少这种情况?我觉得这对我来说很昂贵,因为我们将此服务作为核心服务,用于保持与业务关键文档的同步。
来自社区的任何建议都会有所帮助
小智 5
syncCollection侦听您收藏中每个文档的每次更改。这是很好的开始,但是一旦您的应用程序增长,您就希望更加精确。
syncCollection用于具有列表syncDoc的页面和具有单一视图的页面。并且不要忘记在离开页面时取消订阅(我喜欢为此目的使用 Guards)。getValue。在某些情况下,您不想实时执行读取,例如非协作表单或仅显示一个不应更改的键的列表。在这种情况下,您可以getValue()用于集合或getValue(id)文档。syncCollection(ref => ref.limitTo(10).where(...).作为旁注,我认为这setHasCache不会在这里产生任何影响。Firebase 使用 IndexedDB 来缓存您已经使用过的数据。因此,如果自上次以来没有任何变化,则您无需为此付费。
| 归档时间: |
|
| 查看次数: |
314 次 |
| 最近记录: |