秋田收藏服务中的SyncCollection太贵了?

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侦听您收藏中每个文档的每次更改。这是很好的开始,但是一旦您的应用程序增长,您就希望更加精确。

  1. 仅在需要时同步syncCollection用于具有列表syncDoc的页面和具有单一视图的页面。并且不要忘记在离开页面时取消订阅(我喜欢为此目的使用 Guards)。
  2. 如果你不需要是始终保持最新,可以考虑使用快照,而不是getValue。在某些情况下,您不想实时执行读取,例如非协作表单或仅显示一个不应更改的键的列表。在这种情况下,您可以getValue()用于集合或getValue(id)文档。
  3. 使用 queryFn 侦听较少的文档。大多数情况下,您不想获取整个集合,而只想获取其中的一个子集:syncCollection(ref => ref.limitTo(10).where(...).
  4. 不要太担心价格。您将支付一些欧元/百万次阅读。您花在优化它上的时间将比 firebase 的读取成本花费更多的时间给您的公司。编写代码时要记住价格,但不要花太多时间优化事物;)。

作为旁注,我认为这setHasCache不会在这里产生任何影响。Firebase 使用 IndexedDB 来缓存您已经使用过的数据。因此,如果自上次以来没有任何变化,则您无需为此付费。