angular - Fork 加入两个 firebase observable

标签 angular firebase firebase-realtime-database observable rxjs5

我正在使用 angular2fire。我正在查询并试图从一个城市获得所有旅行。

getAllTours(cityId) {
    return this.af.database.list(`/cities/${cityId}/tours`)
        .map((tours): any => {
            tours.map((tour: any) => {
                tour.tour  = this.af.database.object(`/tours/${tour.$key}/tours`)
            });
            return tours;
        })
}

如果我 console.log 游览对象,我会得到一个“FirebaseObjectObservable”数组。

我必须遍历所有 FirebaseObjectObservable,以获取实际数据。

我想知道我是否可以 forkJoin 所有的 observables 并将输出作为一个带有单个订阅函数的数组。

这是正确的方法吗?

我知道我可以在所有观察者数组上做一个异步管道,但我想在 Controller 中获取数据,然后在它显示在 View 中之前做一些处理,所以异步管道真的不是最好的解决方案我。

最佳答案

是的,forkJoin 可用于获取内部可观察对象的数据:

getAllTours (cityId) {
    return this.af.database
        .list(`/cities/${cityId}/tours`)
        .mergeMap((tours) => {

            // The array of tours is going to be mapped to an observable,
            // so mergeMap is used.

            return Observable.forkJoin(

                // Map the tours to the array of observables that are to
                // be joined. Note that forkJoin requires the observables
                // to complete, so first is used.

                tours.map((tour) => this.af.database
                    .object(`/tours/${tour.$key}/tours`)
                    .first()
                ),

                // Use forkJoin's results selector to match up the result
                // values with the tours.

                (...values) => {
                    tours.forEach((tour, index) => { tour.tour = values[index]; });
                    return tours;
                }
            );
        });
}

使用 forkJoin 是否是正确的方法将取决于您的要求。

在上面的代码中,getAllTours 返回的 observable 将不会发出一个值,直到所有内部 observable 都完成——也就是说,直到每个城市的游览都被查找过。这可能会影响感知性能 - 如果 /cities/${cityId}/tours 中的信息可以在 /tours/${ 中的信息之前显示tour.$key}/tours 被查找,你将无法显示它。同样,您将无法在结果到达时显示城市的游览。

使用 forkJoin 使处理实现更简单,但它可能会使 UI 感觉更慢。 (但是,您可能不希望对 UI 进行零星更新。)

请注意,如果您确实需要在每个城市的游览显示在 View 中之前对其进行一些处理,您可能能够对问题代码中的可观察对象执行上述处理。例如,使用您的 getAllTours 函数:

observable = getAllTours(someCityId);
observable.map((tours) => {

    tours.forEach((tour) => {

        // With your function, tour.tour is an observable, so map
        // could be used to process the values.

        tour.tour = tour.tour.map((value) => {

            // Do some processing here with the value.
        })

        // And, if you are not interested in dynamic updates, you could
        // call first.

        .first();
    });
    return tours;
});

然后您可以在模板中使用 async 管道,它会接收您处理过的游览。

关于angular - Fork 加入两个 firebase observable,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39319673/

相关文章:

javascript - 获取推送后创建的对象的 key (Angular 和 Firebase)

javascript - 我想如何从 firebase 对象获取数据?

ios - 有没有办法安排对 firebase 数据库的编辑?

swift - 如何在 viewDidLoad() 函数上正确读取 Firebase 的数据库?

javascript - 如何正确地为 Angular 中的表实现 ngx-pagination?

angular - RxJS fromEvent 运算符在 Angular 中输出 EventEmitter

angular - 将鼠标悬停在图像上可在其旁边显示更大的图像。 Angular 7

angular - 如何使用异步管道形式的 mat-option 值?

java - 在 Firebase 群发消息应用中实现阅读回执功能

ios - 当尝试注销/放松回到主屏幕时,如果甚至没有 optional ,为什么会收到“找到零”错误?