AngularFire2 在订阅时检查列表是否为空

AngularFire2 check if a list is empty while subscribing

我将 AngularFire2 与 Ionic2 应用程序一起使用。我正在尝试从数据库加载元素列表并在加载时显示微调器。

当列表为空时,我遇到了一个问题,我不知道如何通知控制器停止微调器,因为在新添加之前永远不会有数据。我可能没有正确的结构。

所以这是我服务的摘录:

this.userTourList = this.af.database.list('/userProfiles/' + this.userId + '/tours/');
this.userTourList.subscribe((response) => {
    response.map((tourData) => {
        //Create and fill a TourSnapshot component
        let tourSnap = new TourSnapshot(tourData.name, tourData.$key);
        tourSnap.description = tourData.description;
        //[..]
        this.tours.push(tourSnap);
        //Return an array through my own observable
        return this.toursObservable.next(this.tours);
    });
}

这是我的控制器:

let loader = this.loadingCtrl.create({
    content: "Loading tours..."
});
loader.present().then(() => {
   //The list will be updated automatically
   tourData.getExpertTourList().subscribe(tourList => {
      this.tourList = tourList;
      loader.dismiss().catch(() => {});
   });
});

如果列表为空,我的加载程序将永远不会被关闭(直到完成新添加)。我考虑过在我的服务中对可观察对象使用 isEmpty() 方法,如下所示:

this.userTourList.isEmpty().subscribe(isEmpty => {
//Force a return if the "tours" node is empty to indicate to the controller
//that nothing else will be returned before a new add
    if (isEmpty) {
       return this.userToursObservable.next(this.tours);
    }
});

但我总是得到错误的 isEmpty 值。如何在不额外调用数据库的情况下检查我的列表是否为空? (如果可能的话)。

谢谢

根据@cartant 的评论,我在调用 map() 方法之前添加了对列表大小的检查。

this.userTourList = this.af.database.list('/userProfiles/' + this.userId + '/tours/');
this.userTourList.subscribe((response) => {
    if (response.length == 0) {
       //Return an empty array anyway
       return this.toursObservable.next(this.tours);
    }
    response.map((tourData) => {
        //Create and fill a TourSnapshot component
        let tourSnap = new TourSnapshot(tourData.name, tourData.$key);
        tourSnap.description = tourData.description;
        //[..]
        this.tours.push(tourSnap);
        //Return an array through my own observable
        return this.toursObservable.next(this.tours);
    });
}