简体   繁体   English

如何在Ionic 2中为多个请求显示LoadingController

[英]How do I show LoadingController for multiple requests in Ionic 2

As per the Ionic documentation I could load Ionic LoadingController using this piece of code: 根据Ionic文档,我可以使用以下代码加载Ionic LoadingController:

ionViewLoaded() {
  let loader = this.loading.create({
    content: 'Getting latest entries...',
  });

  loader.present().then(() => {
    this.someService.getLatestEntries()
      .subscribe(res => {
        this.latestEntries = res;
        loader.dismiss();
      });

  });
}

This works fine if I am just making 1 ajax request. 如果我只是发出1个ajax请求,这样可以正常工作。 But I have 5 requests to be made and if I hide / show loading controller like this it leads to flickering of the screens. 但我有5个要求,如果我隐藏/显示加载控制器这样会导致屏幕闪烁。

Is there a way where I can present the loadingController but then dismiss() only when all of the Ajax requests have been completed? 有没有办法我可以提供loadingController,但只有当所有的Ajax请求都已完成时才dismiss()

EDIT: Requests do not depend upon one another. EDIT:请求不依赖于彼此。 They are independent. 他们是独立的。 What I am looking for is a way to get notified when all observable's request is completed. 我正在寻找的是一种在所有可观察的请求完成时得到通知的方法。 That way I could dismiss the LoadingController when all of them complete. 这样,当所有这些都完成时,我可以解除LoadingController。

I'm using following utility in my app. 我在我的应用程序中使用以下实用程序。 This would also work for multiple steps or/and pages if we want to show the same loader and need to change loader text at different steps. 如果我们想要显示相同的加载器并且需要在不同的步骤更改加载器文本,这也适用于多个步骤或/和页面。

We can call showLoader() multiple times, would need to ultimately call hideLoader(). 我们可以多次调用showLoader(),最终需要调用hideLoader()。

@Injectable()
export class Utility {
    loader: any = null;
    constructor(private _alertCtrl: AlertController,
        private _loadingController: LoadingController) {
    }

    private showLoadingHandler(message) {
        if (this.loader == null) {
            this.loader = this._loadingController.create({
                content: message
            });
            this.loader.present();
        } else {
            this.loader.data.content = message;
        }
    }

    private hideLoadingHandler() {
        if (this.loader != null) {
            this.loader.dismiss();
            this.loader = null;
        }
    }

    public showLoader(message) {
        this.showLoadingHandler(message);
    }

    public hideLoader() {
        this.hideLoadingHandler();
    }
}   

You should not use multiple subscribe, and if your requests do not depend on each other you can use the combineLatest operator, so all requests will execute concurently, and get notified when they have all completed : 您不应该使用多个订阅,如果您的请求不相互依赖,则可以使用combineLatest运算符,因此所有请求都将以完整的方式执行,并在完成所有请求后得到通知:

ionViewLoaded() {
  let loader = this.loading.create({
    content: 'Getting latest entries...',
  });
  let loaded = Observable.combineLatest(
    this.someService.getLastEntries().do((entries) => {
      //do stuff with last entries
    }),
    this.someOtherServices.getOtherStuff().do((stuff) => {
      // do stuff with stuff
    })
    // etc...
  )
  loader.present().then(() => loaded.toPromise()).then(() => loader.dismiss());
}

You could chain all the requests inside every subscribe. 您可以在每个订阅中链接所有请求。

ionViewLoaded() {
  let loader = this.loading.create({
    content: 'Getting latest entries...',
  });

  loader.present().then(() => {
    this.someService.getLatestEntries1()
      .subscribe(res => {
        this.latestEntries1 = res;
        this.someService.getLatestEntries2()
          .subscribe(res => {
            this.latestEntries2 = res;
            // OTHER CALLS ...
            this.someService.getLatestEntries5()
              .subscribe(res => {
                this.latestEntries5 = res;
                loader.dismiss();
              });
          });
      });
  });
}

The way you're using the loader.dismiss() is not good, it'll call the someService.getLatestEntries and imediatelly use the dismiss, you need to use it inside the return of your promise/subscribe/observable so it only dismiss when the function is done. 你使用loader.dismiss()方式不好,它会调用someService.getLatestEntries并且使用dismiss,你需要在你的promise / subscribe / observable的返回中使用它,所以它只在功能完成了。

Don't forget about adding an onError with every call, since it's 5 you don't want one to go wrong and then the user get stuck forever. 不要忘记在每次调用时添加一个onError ,因为它是5,你不希望出错,然后用户永远陷入困境。 So add it and inside of every onError add a loader.dismiss(); 所以添加它并在每个onError添加一个loader.dismiss();

Hope this helps :) 希望这可以帮助 :)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM