首页 > 解决方案 > 不断更新数据库

问题描述

我是软件开发的新手,正在构建一个 Angular TS 应用程序来管理队列中的人,我使用 Firebase/Firestore 作为我的后端。用户有 3 种状态:用户正在等待、被呼叫或不在队列中。当用户被呼叫时,他/她必须在 10s 之前回复“出席”,否则他/她会自动被移出队列。

我尝试通过每秒激活一个服务来实现最后一个规范,该服务将检查是否已超过时间限制,并在这种情况下将用户状态更新为相应的 Firestore 文档中的“队列外”。

用户服务.ts

 unqueueUser(): any {
 const actualTime = new Date();

 this.afs.collection<User>('users', ref =>
 ref.orderBy('status.call_moment.time_lim', 'asc')
   .where('status.value', '==', "CALLED")
   .where('status.call_moment.time_lim', '<=', actualTime))
   .snapshotChanges()
   .pipe(map((actions: DocumentChangeAction<User>[]) => {
      const indices = [];
      actions.map((a: DocumentChangeAction<User>) => {
        const data = a.payload.doc.data() as User;
        indices.push(data.id);
      });
      return indices;
    }))
    .subscribe(data => {
      for (const id of data) {
        return this.afs.doc(`users/${id}`)
        .update({
          'status.value': "OUT OF THE LINE",
        });
      }
    });
 }

用户.component.ts

export class UserListComponent implements OnInit {
userList: User[];

ngOnInit() {
  this.userService.getUserList().subscribe(data => this.userList = data);

  setInterval(() => { this.userService.unqueueUser(); }, 1000);
 }
}

我的解决方案中最大的问题是很容易在半天之内达到读取操作的限制(我有一百个用户要处理!)。

你知道更好的解决方案吗?让我知道您是否需要更多信息来解决问题。谢谢 !

标签: angularjsfirebasefirebase-realtime-databasegoogle-cloud-firestoregoogle-cloud-functions

解决方案


与其每秒轮询 10 秒,不如在 10 秒结束时读取,如果有响应,您应该以这种方式获得 10 倍的读取次数。如果您需要它比这更具响应性,请每 5 秒轮询一次。


推荐阅读