如何结合3个firebase observables而不会丢失动态更新 [英] How to combine 3 firebase observables without losing dynamic updates

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

问题描述

我有一个3平行的firebase要求。我需要把它们作为一个单一的对象。代码如下:

 从@ angular / http导入{Http}; 
从angularfire2导入{AngularFireDatabase};
从../model/thread中导入{Thread};
从../model/message中导入{Message};
从../model/participant导入{Participant};

构造函数(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 message:Message [] = data [1];
让参与者:Participant [] = data [2];
让AllUserData = {
线程:线程,
消息:消息,
参与者:参与者
}
返回AllUserData;
))
}

我需要的确切格式是这个接口:

pre $ export interface AllUserData {
参与者:Participant [];
线程:Thread [];
消息:消息[];



$ b $问题是forkJoin不会运行.first()所有的firebase观测。如果我使用.first()来完成所有的firebase observables,我失去了动态更新,为什么我使用firebase的第一位。我怎样才能得到两全其美的呢? - 保持firebase的可观察性,但可以加入他们像我的界面格式?我正在把头发拉出来。任何帮助将不胜感激!

解决方案

您可以使用 .forkJoin(...) .combineLatest(.. 。



我也简化了 map
$ b

  Observable.combineLatest(
this.db.list('participant /') + uid +'/ threads'),
this.db.list('participant /'+ uid +'/ messages'),
this.db.list('participant /'+ uid +' (线程,消息,参与者):[Thread [],Message [],Participant []])=>
({threads,消息,参与者})


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[];
}

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!

解决方案

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

I also did streamline your map a bit:

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天全站免登陆