【问题标题】:How refresh observable every 30 seconds?如何每 30 秒刷新一次 observable?
【发布时间】:2023-04-11 05:09:03
【问题描述】:
@Component({
  selector: 'app-geo',
  templateUrl:   <img  mat-card-image [src]="profileUrl | async ">

  export class GeoComponent implements OnInit {
    date;
    profileUrl: Observable<string>;

    constructor(private tempService: TemperatureService, private humService: HumiditeService, public authService: AuthService,private database: AngularFireDatabase, private storage: AngularFireStorage,private router: Router)
    {
      const ref = this.storage.ref('live/live.jpg');
      this.profileUrl = ref.getDownloadURL(); 
    }
  }
})

我在 Firebase 中的图像每 30 秒覆盖一次。如何每 30 秒获取一次 this.profileUrl 的值并将其保存到 Firebase?

如何使用计时器每 30 秒刷新一次我的 observable?

【问题讨论】:

  • 将计时器设置为每 30 秒通过 HttpClient 从 Firebase 获取新路径,并订阅结果。
  • 我不确定你在这里尝试做什么。但是您知道 valueChanged 会捕获您在数据库文档中所做的每一次更改吗?因此,在这次捕获之后,您可以订阅并设置您的价值。
  • 是的,这就是为什么我会优先在 firebase 中写入下载 url,但我已经确定了位置 //gs...
  • 有人可以告诉我如何使用计时器和 httpsClient 吗?

标签: angular typescript firebase observable angularfire


【解决方案1】:

RxJS IntervalTakeWhile 运算符的组合将完成这项工作。

类似StackBlitz project example code:

const source = interval(30000).pipe(
  takeWhile(() => { return true }) // here you can have some logic to stop the requests
);
const subscription = source.subscribe(
  (x) => {
    // your code goes here
  },
  function (err) {
    console.log('Error: ' + err);
  },
  function () {
    console.log('Completed');
  });

【讨论】:

  • 嗨。谢谢我把这个代码: const ref = this.storage.ref('live/live.jpg'); this.profileUrl = ref.getDownloadURL(); ?
  • 我很乐意提供帮助。使用它的一个重要问题是,当你的组件销毁时,杀死这个观察者,这样它就不会一直触发,直到应用程序被杀死
  • 我该怎么做?实际上,当另一个图像出现时我遇到了一些麻烦......
  • 我更新了示例项目,向您展示如何终止订阅。请注意,当从 main 移动到 child 时,main 停止写入控制台,而当从 child 移动到 main 时,child 仍在写入控制台。您需要关注的代码位于 main.component.ts 中,它正在实现 OnDestroy 并取消订阅观察者。
  • 谢谢我明天会处理这个非常感谢你
猜你喜欢
  • 1970-01-01
  • 2012-06-13
  • 2017-11-08
  • 1970-01-01
  • 2017-07-14
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2022-07-06
相关资源
最近更新 更多