如何在不丢失动态更新的情况下组合 3 个 firebase observables [英] How to combine 3 firebase observables without losing dynamic updates

查看:14
本文介绍了如何在不丢失动态更新的情况下组合 3 个 firebase observables的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要 3 个并行的 Firebase.我需要将它们作为 1 个单一对象加入.代码如下:

I have a 3 parallel firebase requires. And I need to join them as 1 single object. Code as below:

import {Http} from "@angular/http";
import {AngularFireDatabase} from "angularfire2";
import {Thread} from "../model/thread";
import {Message} from "../model/message";
import {Participant} from "../model/participant";

constructor(private  http: Http, private db:AngularFireDatabase) { }

loadUserThreads(uid): Observable<AllUserData> {
return Observable.forkJoin([
  this.db.list('participant/' + uid + '/threads').first(),
  this.db.list('participant/' + uid + '/messages').first(),
  this.db.list('participant/' + uid + '/participants').first()
])
  .map((data:any[]) => {
    let threads: Thread[] = data[0];
    let messages: Message[] = data[1];
    let participants: Participant[] = data[2];
    let AllUserData = {
      threads: threads,
      messages: messages,
      participants: participants
    }
    return AllUserData;
  })
  }

这项工作并返回我需要的确切格式,这是这个接口:

This work and return the exact format I need, which is with this interface:

export interface AllUserData {
  participants: Participant[];
  threads: Thread[];
  messages: Message[];
}

问题是如果没有 .first() 所有 firebase observables,forkJoin 将无法运行.如果我使用 .first() 来完成所有的 firebase observables,我就失去了动态更新,这也是我首先使用 firebase 的原因.我怎样才能做到两全其美?- 保持 firebase observables 活动,但可以像我的界面格式一样加入它们吗?我正在为此拉头发.任何帮助将不胜感激!

The problem is forkJoin won't run without .first() all the firebase observables. And if I use .first() to complete all the firebase observables, I LOST the dynamic updates which why I use firebase the first place. How can I get the best of both world? - Keeping the firebase observables live but can join them like my interface format? I am pulling my hair out on this. Any help will be greatly appreciated!

推荐答案

代替 .forkJoin(...) 你可以使用 .combineLatest(...).

Instead of .forkJoin(...) you could use .combineLatest(...).

我也简化你的map:

Observable.combineLatest(
  this.db.list('participant/' + uid + '/threads'),
  this.db.list('participant/' + uid + '/messages'),
  this.db.list('participant/' + uid + '/participants')
)
.map(([threads, messages, participants]: [Thread[], Message[], Participant[]]) =>
  ({ threads, messages, participants })
)

这篇关于如何在不丢失动态更新的情况下组合 3 个 firebase observables的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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