gpt4 book ai didi

firebase - 如何在不丢失动态更新的情况下组合 3 个 firebase observable

转载 作者:行者123 更新时间:2023-12-02 20:05:15 26 4
gpt4 key购买 nike

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

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;
})
}

此工作并返回我需要的确切格式,这是使用此接口(interface)的:

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

问题是如果没有 .first() 所有 firebase observables,forkJoin 将无法运行。如果我使用 .first() 来完成所有 firebase observables,我会丢失动态更新,这就是我首先使用 firebase 的原因。我怎样才能两全其美? - 保持 firebase observables 活跃,但可以像我的界面格式一样加入它们?我对此很抓狂。任何帮助将不胜感激!

最佳答案

您可以使用 .combineLatest(...) 而不是 .forkJoin(...) .

我还简化了你的 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 })
)

关于firebase - 如何在不丢失动态更新的情况下组合 3 个 firebase observable,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41785252/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com